vs2005 DataGridView 多维合并标题 2点修改

vs2005 DataGridView 多维合并标题 2点修改:
1. 删除自定义数据View时,代码中会残留在View中添加的TreeView对象以及相关代码。
2. 在有行标题的情况下,发生参数无效的异常。

1 - 解决方法:在自定义View代码中添加下一段的Dispose方法。

     重写Dispose,在删除View时,释放所有在View中以添加的ColumnTreeView。

         Protected   Overrides   Sub Dispose(ByVal disposing As Boolean)
            
If _columnTreeView Is Nothing Then
                
MyBase.Dispose(disposing)
                
Return
            
End If
            
For i As Integer = 0 To _columnTreeView.Length - 1
                _columnTreeView(i).Dispose()
            
Next
            
MyBase.Dispose(disposing)
        
End Sub

 2 - 解决方法:多了一个无用代码,删除就可以了。多余代码为PaintUnitHeader方法的 Me.Update()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
DataGridView是.NET平台中的一个控件,用于显示和编辑数据的表格。横向合并单元格是指将相邻的单元格在水平方向上合并成一个单元格,以便显示更复杂的数据结构或提高表格的可读性。 要实现DataGridView的横向合并单元格功能,可以通过以下步骤进行操作: 1. 设置合并单元格的方式: - 设置DataGridView的ColumnHeadersDefaultCellStyle属性的WrapMode为DataGridViewTriState.True,以允许列标题文本换行。 - 设置DataGridView的CellBorderStyle属性为DataGridViewCellBorderStyle.Single,以确保单元格边框可见。 2. 合并单元格: - 使用DataGridView的CellPainting事件来自定义绘制单元格。 - 在CellPainting事件中,判断当前绘制的单元格是否需要合并。 - 如果需要合并,可以使用Graphics对象的FillRectangle方法绘制合并后的单元格背景,并使用DrawString方法绘制合并后的文本。 下面是一个示例代码,演示如何实现DataGridView的横向合并单元格功能: ```csharp private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex >= 0 && e.ColumnIndex >= 0) { DataGridView dgv = (DataGridView)sender; DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex]; // 判断是否需要合并单元格 if (e.ColumnIndex > 0 && cell.Value == dgv.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value) { e.AdvancedBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.None; } else { e.AdvancedBorderStyle.Right = dgv.AdvancedCellBorderStyle.Right; } // 绘制合并后的单元格背景 using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor)) { e.Graphics.FillRectangle(backColorBrush, e.CellBounds); } // 绘制合并后的文本 if (e.Value != null) { using (Brush foreColorBrush = new SolidBrush(e.CellStyle.ForeColor)) { e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, foreColorBrush, e.CellBounds.X + 2, e.CellBounds.Y + 2, StringFormat.GenericDefault); } } e.Handled = true; } } ``` 这样,当DataGridView中相邻的单元格具有相同的值时,它们将被合并成一个单元格。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值