![](/img/trans.png)
[英]Change Font Color of a Column in a DataGridView Control (C# winforms)
[英]Change color of specific part of DataGridView or entire column in C#
是否可以通过代码更改DataGridView
的此部分的颜色?
如果没有,如何更改整个DataGridViewRow
的颜色? 我尝试了这段代码,但是它不会改变颜色:
DataGridViewRow row = new DataGridViewRow();
row.DefaultCellStyle.BackColor = Color.Green;
row.DefaultCellStyle.ForeColor = Color.Green;
作为信息,我的列是DataGridViewComboBoxColumn
您可以通过设置DefaultCellStyle属性来设置行颜色。 在下面的示例中,我们遍历每行并检查cell [0]的值,如果条件为true,则设置行的backcolor和forecolor
foreach (DataGridViewRow row in mydataGridView.Rows)
{
string RowType = row.Cells[0].Value.ToString();
if (RowType == "Some Value")
{
row.DefaultCellStyle.BackColor = Color.Green;
row.DefaultCellStyle.ForeColor = Color.Green;
}
}
如果您要说更改蓝色背景 ,则蓝色背景是gridview中选定行的默认颜色。 您可以在属性窗口中更改此颜色
Gridview1.DefaultCellStyle.SelectionBackColor = Color.Red; or Color.Transparent
Gridview1.DefaultCellStyle.SelectionForeColor = Color.Black; or Color.Transparent
但是,如果您要在很短的时间内刷新网格视图,请说少于1秒,那么在这种情况下,您必须更改Gridview中添加的行单元格的默认颜色(高于senario可以在静态网格视图中正常使用)。
Gridview1.Rows[i].DefaultCellStyle.SelectionBackColor = Color.Red; or Color.Transparent
Gridview1.Rows[i].DefaultCellStyle.SelectionForeColor = Color.Black;or Color.Transparent
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.