当前位置:编程学习 > C#/ASP.NET >>

在gridview里面一个字段显示3列 一列为今年的数据,后面的显示去年数据



E_produNum 是能源消耗量  

现在是要求 E_produNum 这个能源消耗 显示今年和去年 的消耗量 E_produNum1 E_produNum2 都是E_produNum这个字段的数据
 下面是我自己写的 不过不对 只是给你们看看 方便你们修改

SELECT e_companyid,username,user1.id,E_produNum, max(CASE WHEN e_energy = '原煤' THEN E_produNum END) [E_produNum1],max(CASE WHEN e_energy = '原煤' and e_setdate='200908' THEN E_produNum END) [E_produNum2]  FROM table_1 inner join user1 on e_companyid=qiyibh where e_setdate='201008' and del='n' group by e_companyid,username,user1.id,E_produNum  order by user1.id
--------------------编程问答-------------------- 可以把三列合并成一列,具体参照下面的代码:

        /// <summary> 
        /// 合并表头,用在dataGridView的CellPainting事件中。 
        /// </summary> 
        /// <param name="sender">需要重绘的dataGridview</param> 
        /// <param name="e">CellPainting中的参数</param> 
        ///<param name="colName">列的集合(列必须是连续的,第一列放在最前面)</param> 
        /// <param name="headerText">列合并后显示的文本</param> 
        public void MergeHeader(object sender, DataGridViewCellPaintingEventArgs e, List<string> colNameCollection, string headerText)
        {
            if (e.RowIndex == -1 && e.ColumnIndex != -1)
            {
                DataGridView dataGridView1 = sender as DataGridView;
                string colName = dataGridView1.Columns[e.ColumnIndex].Name;

                //0.扩展表头高度为当前的2倍 
                if (!hs.Contains(dataGridView1.Name))
                {
                    hs.Add(dataGridView1.Name, "0");
                    dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                   // dataGridView1.ColumnHeadersHeight = e.CellBounds.Height * 2;
                    dataGridView1.ColumnHeadersHeight = e.CellBounds.Height ;
                }

                if (colNameCollection.Contains(colName))
                {
                    #region 重绘列头
                    //1.计算colLen个列的区域 
                    if (colNameCollection.IndexOf(colName) == 0)
                    {
                        cTop = e.CellBounds.Top;
                        cLeft = e.CellBounds.Left;

                        cWidth = e.CellBounds.Width;
                        //cHeight = e.CellBounds.Height / 2;
                        cHeight = e.CellBounds.Height ;
                        //求总的合并列的宽度
                        foreach (string colNameItem in colNameCollection)
                        {
                            if (colNameItem.Equals(colName))
                            {//除去自己一个,加了之后colLen-1个列的宽 
                                continue;
                            }
                            cWidth += dataGridView1.Columns[colNameItem].Width;
                        }
                    }
                    if (colNameCollection.IndexOf(colName) == 0)
                    {
                        Rectangle cArea = new Rectangle(cLeft, cTop, cWidth, cHeight);
                        //2.把区域设置为背景色,没有列的分线及任何文字。 
                        using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                        {
                            e.Graphics.FillRectangle(backColorBrush, cArea);

                        }
                    }
                    if (colNameCollection.IndexOf(colName) == 0)
                    {
                        //3.绘制新列头的边框 
                        using (Pen gridPen = new Pen(dataGridView1.GridColor))
                        {
                            //3.1 上部边框 
                            e.Graphics.DrawLine(gridPen, cLeft, cTop, cLeft + cWidth, cTop);
                            using (Pen hilightPen = new Pen(Color.WhiteSmoke))
                            {
                                //3.2 顶部高光 
                                e.Graphics.DrawLine(hilightPen, cLeft, cTop + 1, cLeft + cWidth, cTop + 1);
                                //3.3 左部反光线 
                                e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3, cLeft, cTop + cHeight - 2);
                            }
                            //3.4 下部边框 
                            e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1, cLeft + cWidth, cTop + cHeight - 1);
                            //3.5 右部边框                         
                            e.Graphics.DrawLine(gridPen, cLeft + cWidth - 1, cTop, cLeft + cWidth - 1, cTop + cHeight);//(cTop+cHeight)/2); 
                        }
                    }

                    //4.Header 写文本 
                    if (colNameCollection.IndexOf(colName) == 0)
                    {//不是第一列则不写文字。 

                        int wHeadStr = (int)(headerText.Length * e.CellStyle.Font.SizeInPoints);
                        int wHeadCell = cWidth;
                        int pHeadLeft = (wHeadCell - wHeadStr) / 2 - 6;
                        using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))
                        {
                            e.Graphics.DrawString(headerText, e.CellStyle.Font, foreBrush, new PointF(cLeft + pHeadLeft, cTop + 3));
                        }
                    }


                    ////5 绘制子列背景 
                 /*       int FatherColHeight = e.CellBounds.Height / 2;//上面一行的高度  
                             using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                            {
                                e.Graphics.FillRectangle(backColorBrush, new Rectangle(e.CellBounds.X, e.CellBounds.Y + FatherColHeight, e.CellBounds.Width - 1, e.CellBounds.Height / 2 - 1));
                            }
                             ////5.1绘制子列的边框

                            using (Pen gridPen = new Pen(dataGridView1.GridColor))
                            {
                                using (Pen hilightPen = new Pen(Color.WhiteSmoke))
                                {
                                    //5.2 左部反光线 
                                    e.Graphics.DrawLine(hilightPen, cLeft, cTop + 3 + FatherColHeight, cLeft, cTop + cHeight - 2 + FatherColHeight);
                                }
                                //5.3 下部边框 
                                e.Graphics.DrawLine(gridPen, cLeft, cTop + cHeight - 1 + FatherColHeight, cLeft + cWidth, cTop + cHeight - 1 + FatherColHeight);

                                //5.4 右部边框  
                                e.Graphics.DrawLine(gridPen, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Top + FatherColHeight, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Top + e.CellBounds.Height + FatherColHeight);//(cTop+cHeight)/2);                     

                            }
                            //5.5 写子列的文本 
                            int wStr = (int)(dataGridView1.Columns[e.ColumnIndex].HeaderText.Length * e.CellStyle.Font.SizeInPoints);
                            int wCell = e.CellBounds.Width;
                            int pLeft = (wCell - wStr) / 2;//相对CELL左边框的左坐标 

                            using (Brush foreBrush = new SolidBrush(e.CellStyle.ForeColor))
                            {
                                e.Graphics.DrawString(dataGridView1.Columns[e.ColumnIndex].HeaderText, e.CellStyle.Font, foreBrush, new PointF(e.CellBounds.X + pLeft, cTop + 3 + FatherColHeight));
                            }*/

                    #endregion
                    e.Handled = true; //系统继续处理
                }
            }
        }

        /// <summary>
        /// 扩展表头高度为当前的2倍
        /// </summary>
        /// <param name="sender"></param>
        public void HeightHeader(object sender, DataGridViewCellPaintingEventArgs e)
        {
            DataGridView dataGridView1 = sender as DataGridView;
            if (!hs.Contains(dataGridView1.Name))
            {
                hs.Add(dataGridView1.Name, "0");
                dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
               // dataGridView1.ColumnHeadersHeight = e.CellBounds.Height * 2;
                dataGridView1.ColumnHeadersHeight = e.CellBounds.Height ;
            }
        }
--------------------编程问答-------------------- 我是要把1列里面的数据分成3列  然后后面2列是去年的数据   我不是很明白你这个效果啊.. --------------------编程问答-------------------- 是这样的,它是在数据库里面以三列的形式存储,但是在datagridview当中以三列共用一个列名来显示的 --------------------编程问答-------------------- 就是说你可以先查出来前年的那两列数据存放到dataset中去,再以dataset作为你的DataGridView的数据源,在Datagridview中有CellPainting这个事件,在这里面调用上面的函数就可以做到把那三个列用共同的列名显示出来,不知道这样说,楼主明白一些没有? --------------------编程问答-------------------- 具体效果如下:
--------------------编程问答-------------------- 看不到图 呢? --------------------编程问答-------------------- lz,你把你邮箱给我,我把效果图给你发过去,往csdn上发图片,我还真的不太会,不好意思啊 --------------------编程问答-------------------- gaoqh2009@163.com   昨天 去看医生了 会来就直接睡了  今天还是很难受啊 --------------------编程问答-------------------- 楼主,还是需要注意一下自己的身体,等没事的时候,再看一眼我这个效果图,希望对你有帮助,因为前段时间用过这个东西,所以看到你也需要就直接发上来了,抽空看下邮箱,我这就给你发过去 --------------------编程问答-------------------- LZ,图片已发送,抽空接收一下 --------------------编程问答--------------------  我已经看到了 但需要的效果不一样..麻烦你 你有这样的实例么 可以查看到今年和去年的数据 是同一家公司的数据 有的话给我参考下  谢谢
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,