属性"visible"为"readonly"
时间: 2023-06-29 18:13:53 浏览: 80
非常抱歉,我的回答依然有误。DataGridViewCell类中确实有Visible属性,但是这个属性是只读的,不能通过设置来控制单元格的显示和隐藏。所以,实现DataGridView单元格合并的方法是使用DataGridView的CellPainting事件。以下是示例代码:
```
Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
'合并第0列相邻的相同单元格
If e.ColumnIndex = 0 AndAlso e.RowIndex <> -1 Then
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None '去掉下边框线
If e.RowIndex < DataGridView1.Rows.Count - 1 AndAlso DataGridView1.Rows(e.RowIndex).Cells(0).Value = DataGridView1.Rows(e.RowIndex + 1).Cells(0).Value Then
e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None '去掉上边框线
Else
e.AdvancedBorderStyle.Top = DataGridView1.AdvancedCellBorderStyle.Top '还原上边框线
End If
e.PaintBackground(e.CellBounds, True)
e.PaintContent(e.CellBounds)
If e.RowIndex < DataGridView1.Rows.Count - 1 AndAlso DataGridView1.Rows(e.RowIndex).Cells(0).Value = DataGridView1.Rows(e.RowIndex + 1).Cells(0).Value Then
e.Handled = True '隐藏当前单元格
Else
e.Handled = False '显示当前单元格
End If
End If
End Sub
```
这段代码实现了DataGridView单元格的合并,具体实现方法是在CellPainting事件中进行处理。当单元格的列索引为0且行索引不为-1时,即为合并的单元格。在处理过程中,我们先判断当前单元格和下一行单元格的值是否相等,如果相等,则隐藏当前单元格,同时将当前单元格的上边框线去掉,从而实现单元格的合并。如果不相等,则显示当前单元格,同时还原当前单元格的上边框线。这样,就可以实现DataGridView单元格的合并。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)