[英]Changing datagridview cell color dynamically
I have a dataGridView object that is populated with data.我有一个填充了数据的 dataGridView object。 I want to click a button and have it change the color of the background of the cell.
我想单击一个按钮并让它更改单元格背景的颜色。 This is what I currently have
这就是我目前拥有的
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?问:如何在表格存在后更改单个单元格的背景颜色?
这对我有用
dataGridView1.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Red;
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()});
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++;
}
}
如果您希望网格中的每个单元格都具有相同的背景颜色,您可以这样做:
dataGridView1.DefaultCellStyle.BackColor = Color.Green;
Considere use DataBindingComplete event for update the style.考虑使用 DataBindingComplete 事件来更新样式。 The next code change the style of the cell:
下一个代码更改单元格的样式:
private void Grid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
this.Grid.Rows[2].Cells[1].Style.BackColor = Color.Green;
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
row.Cells[col.Index].Style.BackColor = Color.Green;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.