C#用代码设置DataGridView属性时,怎么在DataGridViewButtonCell按钮中添加图标,有例子吗?
时间: 2023-11-27 14:55:23 浏览: 39
在C#中设置DataGridViewButtonCell按钮中添加图标可以通过以下代码实现:
```csharp
// 创建一个DataGridViewButtonColumn列
DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
buttonColumn.Name = "buttonColumn";
buttonColumn.HeaderText = "操作";
buttonColumn.Text = "点击";
buttonColumn.UseColumnTextForButtonValue = true;
// 设置列中的按钮图标
Image icon = Image.FromFile("icon.png");
DataGridViewImageCell cell = new DataGridViewImageCell();
cell.Value = icon;
cell.ImageLayout = DataGridViewImageCellLayout.Zoom;
buttonColumn.CellTemplate = cell;
// 将列添加到DataGridView控件中
dataGridView1.Columns.Add(buttonColumn);
```
这段代码创建了一个名为`buttonColumn`的`DataGridViewButtonColumn`列,并且设置了该列中的按钮图标为`icon.png`,然后将该列添加到`dataGridView1`控件中。