问题描述
C#.NETDataGridView请各位大虾帮帮忙,我想在已经数据的DataGridView上,把同一列下相同的单元格合并,有什么办法吗?请指教,或者有相关单元格合并的例子,可以参考一下!
解决方案
解决方案二:
"#region"合并单元格的测试"privateintnextrow=null;privateintnextcol=null;privatevoiddataGridView1_CellFormatting(objectsender,System.Windows.Forms.DataGridViewCellFormattingEventArgse){if(this.dataGridView1.Columns["description"].Index==e.ColumnIndex&&e.RowIndex>=0){if(this.nextcol!=null&e.ColumnIndex==this.nextcol){e.CellStyle.BackColor=Color.LightBlue;this.nextcol=null;}if(this.nextrow!=null&e.RowIndex==nextrow){e.CellStyle.BackColor=Color.LightPink;this.nextrow=null;}if(e.RowIndex!=this.dataGridView1.RowCount-1){if(e.Value.ToString()==this.dataGridView1.Rows[e.RowIndex+1].Cells[e.ColumnIndex].Value.ToString()){e.CellStyle.BackColor=Color.LightPink;nextrow=e.RowIndex+1;}}}if(this.dataGridView1.Columns["name"].Index==e.ColumnIndex&&e.RowIndex>=0){if(e.Value.ToString()==this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex+1].Value.ToString()){e.CellStyle.BackColor=Color.LightBlue;nextcol=e.ColumnIndex+1;}}}//==========================//绘制单元格privatevoiddataGridView1_CellPainting(objectsender,System.Windows.Forms.DataGridViewCellPaintingEventArgse){//纵向合并if(this.dataGridView1.Columns["description"].Index==e.ColumnIndex&&e.RowIndex>=0){using(BrushgridBrush=newSolidBrush(this.dataGridView1.GridColor),backColorBrush=newSolidBrush(e.CellStyle.BackColor)){using(PengridLinePen=newPen(gridBrush)){//擦除原单元格背景e.Graphics.FillRectangle(backColorBrush,e.CellBounds);/**/////绘制线条,这些线条是单元格相互间隔的区分线条,////因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条if(e.RowIndex!=this.dataGridView1.RowCount-1){if(e.Value.ToString()!=this.dataGridView1.Rows[e.RowIndex+1].Cells[e.ColumnIndex].Value.ToString()){e.Graphics.DrawLine(gridLinePen,e.CellBounds.Left,e.CellBounds.Bottom-1,e.CellBounds.Right-1,e.CellBounds.Bottom-1);//下边缘的线//绘制值if(e.Value!=null){e.Graphics.DrawString((String)e.Value,e.CellStyle.Font,Brushes.Crimson,e.CellBounds.X+2,e.CellBounds.Y+2,StringFormat.GenericDefault);}}}else{e.Graphics.DrawLine(gridLinePen,e.CellBounds.Left,e.CellBounds.Bottom-1,e.CellBounds.Right-1,e.CellBounds.Bottom-1);//下边缘的线//绘制值if(e.Value!=null){e.Graphics.DrawString((String)e.Value,e.CellStyle.Font,Brushes.Crimson,e.CellBounds.X+2,e.CellBounds.Y+2,StringFormat.GenericDefault);}}//右侧的线e.Graphics.DrawLine(gridLinePen,e.CellBounds.Right-1,e.CellBounds.Top,e.CellBounds.Right-1,e.CellBounds.Bottom-1);e.Handled=true;}}}//横向合并if(this.dataGridView1.Columns["name"].Index==e.ColumnIndex&&e.RowIndex>=0){using(BrushgridBrush=newSolidBrush(this.dataGridView1.GridColor),backColorBrush=newSolidBrush(e.CellStyle.BackColor)){using(PengridLinePen=newPen(gridBrush)){//擦除原单元格背景e.Graphics.FillRectangle(backColorBrush,e.CellBounds);if(e.Value.ToString()!=this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex+1].Value.ToString()){//右侧的线e.Graphics.DrawLine(gridLinePen,e.CellBounds.Right-1,e.CellBounds.Top,e.CellBounds.Right-1,e.CellBounds.Bottom-1);//绘制值if(e.Value!=null){e.Graphics.DrawString((String)e.Value,e.CellStyle.Font,Brushes.Crimson,e.CellBounds.X+2,e.CellBounds.Y+2,StringFormat.GenericDefault);}}//下边缘的线e.Graphics.DrawLine(gridLinePen,e.CellBounds.Left,e.CellBounds.Bottom-1,e.CellBounds.Right-1,e.CellBounds.Bottom-1);e.Handled=true;}}}}#endregion
解决方案三:
dingy8ixia
解决方案四:
楼主看下这个.前几天查这个问题的看到了这个贴.比较不错`