[WinForm]Datagridview 实现二维表头

public class DataGridViewHelper
     {
         public DataGridViewHelper(DataGridView gridview)
         {
             gridview.CellPainting += new DataGridViewCellPaintingEventHandler(gridview_CellPainting);
         }
         int top = 0;
         int left = 0;
         int height = 0;
         int width1 = 0;
         public void gridview_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
         {
             #region 重绘datagridview表头
             DataGridView dgv = (DataGridView)(sender);
             if (e.RowIndex != -1) return;
             foreach (TopHeader item in Headers)
             {
                 if (e.ColumnIndex >= item.Index && e.ColumnIndex < item.Index + item.Span)
                 {
                     if (e.ColumnIndex == item.Index)
                     {
                         top = e.CellBounds.Top;
                         left = e.CellBounds.Left;
                         height = e.CellBounds.Height;
                     }
                     int width = 0;
                     for (int i = item.Index; i < item.Span + item.Index; i++)
                     {
                         width += dgv.Columns[i].Width;
                     }
                     Rectangle rect = new Rectangle(left, top, width, e.CellBounds.Height);
                     using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                     {
                         //抹去原来的cell背景
                         e.Graphics.FillRectangle(backColorBrush, rect);
                     }
                     using (Pen gridLinePen = new Pen(dgv.GridColor))
                     {
                         e.Graphics.DrawLine(gridLinePen, left, top, left + width, top);
                         e.Graphics.DrawLine(gridLinePen, left, top + height / 2, left + width, top + height / 2);
                         width1 = 0;
                         e.Graphics.DrawLine(gridLinePen, left, top, left, top + height);
                         for (int i = item.Index; i < item.Span + item.Index; i++)
                         {
                             width1 += dgv.Columns[i].Width;
                             e.Graphics.DrawLine(gridLinePen, left + width1, top + height / 2, left + width1, top + height);
                         }
                         SizeF sf = e.Graphics.MeasureString(item.Text, e.CellStyle.Font);
                         float lstr = (width - sf.Width) / 2;
                         float rstr = (height / 2 - sf.Height) / 2;
                         //画出文本框
                         if (item.Text != "")
                         {
                             e.Graphics.DrawString(item.Text, e.CellStyle.Font,
                                                        new SolidBrush(e.CellStyle.ForeColor),
                                                          left + lstr,
                                                          top + rstr,
                                                          StringFormat.GenericDefault);
                         }
                         width = 0;
                         width1 = 0;
                         for (int i = item.Index; i < item.Span + item.Index; i++)
                         {
                             string columnValue = dgv.Columns[i].HeaderText;
                             width1 = dgv.Columns[i].Width;
                             sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
                             lstr = (width1 - sf.Width) / 2;
                             rstr = (height / 2 - sf.Height) / 2;
                             if (columnValue != "")
                             {
                                 e.Graphics.DrawString(columnValue, e.CellStyle.Font,
                                                            new SolidBrush(e.CellStyle.ForeColor),
                                                              left + width + lstr,
                                                              top + height / 2 + rstr,
                                                              StringFormat.GenericDefault);
                             }
                             width += dgv.Columns[i].Width;
                         }
                     }
                     e.Handled = true;
                 }
             }
             #endregion
         }
         private List<TopHeader> _headers = new List<TopHeader>();
         public List<TopHeader> Headers
         {
             get { return _headers; }
         }
 
         public struct TopHeader
         {
             public TopHeader(int index, int span, string text)
             {
                 this.Index = index;
                 this.Span = span;
                 this.Text = text;
             }
             public int Index;
             public int Span;
             public string Text;
         }
     }

使用:

             DataTable dt = new DataTable();
             dt.Columns.Add("id", typeof(int));
             dt.Columns.Add("sum", typeof(int));
             dt.Columns.Add("product", typeof(string));
             dt.Rows.Add(1, 2, "milk");
             dt.Rows.Add(3, 4, "pig");
             dataGridView1.DataSource = dt;
             DataGridViewHelper rowMergeView = new DataGridViewHelper(dataGridView1);
             rowMergeView.Headers.Add(new DataGridViewHelper.TopHeader(1, 2, "总计"));

转自http://www.cnblogs.com/tewuapple/archive/2012/09/05/2671560.html

厦门德仔 CSDN认证博客专家 项目实施 软件开发 数据分析
毕业于南昌大学,英语6级,拥有超过13年的工作经验,数据库工程师(中级),ERP认证工程师,FRCA报表工程师,美国PMP项目管理师,具有项目管理能力,拥有多年项目实施与运维,精通SQL数据库,Asp.net和数据分析管理和开发。曾在台企,港企,美企,民企实施过多个完整ERP,WMS,EHR,OA,BI等项目。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 点我我会动 设计师:上身试试 返回首页
实付 99.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值