c# datagridview合并整列
时间: 2023-07-22 10:22:40 浏览: 54
您可以使用以下代码来合并一个 DataGridView 的整列:
```csharp
private void MergeColumn(DataGridView dataGridView, int columnIndex)
{
string previousValue = dataGridView[columnIndex, 0].Value.ToString();
for (int i = 1; i < dataGridView.Rows.Count; i++)
{
DataGridViewCell cell = dataGridView[columnIndex, i];
if (cell.Value.ToString() == previousValue)
{
cell.Style.BackColor = Color.White;
cell.Style.ForeColor = Color.Black;
cell.Value = "";
}
else
{
previousValue = cell.Value.ToString();
}
}
}
```
您可以将此方法绑定到一个按钮的 Click 事件上,以便在需要时调用它。例如:
```csharp
private void btnMergeColumn_Click(object sender, EventArgs e)
{
MergeColumn(dataGridView1, 0); // 合并第一列
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)