编辑:尝试在 Winforms 中执行以下代码。
我正在从 xml 中获取数据,它们使用我创建的自定义类显示在 datagridview 中。我已将每一列链接到 DataPropertyName
我想在数据网格中显示数据 GridView 文本框、按钮列、复选框,如下图所示。
我正在使用以下事件来更改我添加的按钮的颜色。对于特定元素,假设我将 dt
绑定(bind)到单行网格,然后
if(dt.val=="true")
{
// change the color of that button
}
我正在使用以下代码。
private void Grid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
DataGridViewColumn dt = Grid.Columns[9]; // 9 is column no
foreach (DataGridViewRow r in Grid.Rows)
{
if (newList[r.Index].val.ToString() == "true") //some condition
{
r.DefaultCellStyle = red; // this turns compete row red
// add something here to make button red of this row
}
else
{
r.DefaultCellStyle = green;
// add something here to make button red of this row
}
}
}
- 我无法更改特定单元格按钮的颜色。
- 我如何在最后一行添加复选框,因为我已经添加了 DataGridViewCheckboxColumn 但默认情况下网格不显示任何列。
最佳答案
您可以像这样更改特定单元格的背景颜色:
r.Cells(9).Style.BackColor = Drawing.Color.Red
关于C# Winforms DatagridView - 为不同的行设置不同的按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9161260/