Datagridview指定列进行合并单元格

   private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
   {
       // 仅处理第一列且为数据行
       if (e.ColumnIndex == 0 && e.RowIndex >= 0)
       {
           Brush datagridBrush = new SolidBrush(dataGridView1.GridColor);
           SolidBrush grouplinebBrush = new SolidBrush(e.CellStyle.BackColor);
           using (Pen datagridlinePen = new Pen(datagridBrush))
           {
               e.Graphics.FillRectangle(grouplinebBrush, e.CellBounds);
               if (e.RowIndex < dataGridView1.RowCount - 1 && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value != null && dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString() != e.Value.ToString())
               {
                   e.Graphics.DrawLine(datagridlinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
                   e.Graphics.DrawLine(datagridlinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
               }
               else
               {
                   e.Graphics.DrawLine(datagridlinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
               }
               if (e.RowIndex == dataGridView1.Rows.Count - 1)
               {
                   e.Graphics.DrawLine(datagridlinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
               }

               if (e.Value != null)
               {
                   if (!(e.RowIndex > 0 && dataGridView1.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString() == e.Value.ToString()))
                   {
                       e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Black, e.CellBounds.X + 2, e.CellBounds.Y + 5, StringFormat.GenericDefault);
                   }
               }

               e.Handled = true;
           }

       }
   }

你可能感兴趣的:(C#编程,前端,javascript,开发语言)