C# 动态更改 datagridview 单元格颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17728009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing datagridview cell color dynamically
提问by fifamaniac04
I have a dataGridView object that is populated with data. I want to click a button and have it change the color of the background of the cell. This is what I currently have
我有一个填充数据的 dataGridView 对象。我想单击一个按钮并让它更改单元格背景的颜色。这是我目前拥有的
foreach(DataGridViewRow row in dataGridView1.Rows)
{
foreach(DataGridViewColumn col in dataGridView1.Columns)
{
//row.Cells[col.Index].Style.BackColor = Color.Green; //doesn't work
//col.Cells[row.Index].Style.BackColor = Color.Green; //doesn't work
dataGridView1[col.Index, row.Index].Style.BackColor = Color.Green; //doesn't work
}
}
ALL of these three cause the table to be redrawn over itself in an overlapping manner and trying to re-size the tables becomes a mess. when clicking on a cell, the value remains highlighted and the backcolor doesn't change.
所有这三个都会导致表格以重叠的方式重新绘制在其自身上,并且尝试重新调整表格的大小变得一团糟。单击单元格时,该值保持突出显示并且背景颜色不会改变。
Q: How can I change the backcolor of an individual cell after the table exists?
问:表格存在后,如何更改单个单元格的背景色?
采纳答案by Ehsan
This works for me
这对我有用
dataGridView1.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Red;
回答by Rakesh
Thanks it working
感谢它的工作
here i am done with this by qty field is zero means it shown that cells are in red color
在这里我完成了 qty 字段为零意味着它显示单元格为红色
int count = 0;
foreach (DataGridViewRow row in ItemDg.Rows)
{
int qtyEntered = Convert.ToInt16(row.Cells[1].Value);
if (qtyEntered <= 0)
{
ItemDg[0, count].Style.BackColor = Color.Red;//to color the row
ItemDg[1, count].Style.BackColor = Color.Red;
ItemDg[0, count].ReadOnly = true;//qty should not be enter for 0 inventory
}
ItemDg[0, count].Value = "0";//assign a default value to quantity enter
count++;
}
}
回答by Pavel
Implement your own extension of DataGridViewTextBoxCell and override Paint method like this:
实现您自己的 DataGridViewTextBoxCell 扩展并覆盖 Paint 方法,如下所示:
class MyDataGridViewTextBoxCell : DataGridViewTextBoxCell
{
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,
DataGridViewElementStates cellState, object value, object formattedValue, string errorText,
DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
if (value != null)
{
if ((bool) value)
{
cellStyle.BackColor = Color.LightGreen;
}
else
{
cellStyle.BackColor = Color.OrangeRed;
}
}
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value,
formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}
}
}
Then in the code set CellTemplate property of your column to instance of your class
然后在代码中将列的 CellTemplate 属性设置为类的实例
columns.Add(new DataGridViewTextBoxColumn() {CellTemplate = new MyDataGridViewTextBoxCell()});
回答by ángel Ibá?ez
Considere use DataBindingComplete event for update the style. The next code change the style of the cell:
考虑使用 DataBindingComplete 事件来更新样式。下一个代码更改单元格的样式:
private void Grid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
this.Grid.Rows[2].Cells[1].Style.BackColor = Color.Green;
}