求助, DataGridView中如何动态实现指定的单元格合并?
求助, DataGridView中如何动态实现指定的单元格合并?看看以下的程式码
我的标题是动态的
它能1-3还是1-5 ,还是1氮
问题1
在cellpaint事件是否能用for循环?如何写?
问题2
只需格A1横向合并,其他无需,怎做?
图片结果: http://i105.
程式下载: http://cid-7aa8ce26bddc5545.skydrive.
程序代码:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim dt As New DataTable dt.Columns.Add("Car") dt.Columns.Add("1") dt.Columns.Add("2") dt.Columns.Add("3") dt.Rows.Add(New Object() {"Toyoda", "A1", "C1", "C1"}) dt.Rows.Add(New Object() {"Honta", "A1", "A1", "D2"}) dt.Rows.Add(New Object() {"Falali", "A1", "A1", "A1"}) dt.Rows.Add(New Object() {"Nisun", "B1", "A1", "A1"}) DataGridView1.DataSource = dt End Sub Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting If DataGridView1.Rows.Count > 0 Then 'If Me.DataGridView1.Columns("1").Index = e.ColumnIndex AndAlso e.RowIndex >= 0 Then If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then Using gridBrush As Brush = New SolidBrush(Me.DataGridView1.GridColor), backColorBrush As Brush = New SolidBrush(e.CellStyle.BackColor) Using gridLinePen As New Pen(gridBrush) ' 擦除原单元格背景 e.Graphics.FillRectangle(backColorBrush, e.CellBounds) If e.Value.ToString() <> Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex + 1).Value.ToString() Then '右侧的线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1) '绘制值 If e.Value IsNot Nothing Then e.Graphics.DrawString(DirectCast(e.Value, [String]), e.CellStyle.Font, Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault) End If End If '下边缘的线 e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1) e.Handled = True End Using End Using End If End If End Sub