C# DataGridView 更改单元格背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16105718/
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
DataGridView changing cell background color
提问by Rémi
I have the following code :
我有以下代码:
private void dgvStatus_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in dgvStatus.Rows)
{
row.Cells[color.Index].Style.BackColor = Color.FromArgb(((GesTest.dsEssais.FMstatusAnomalieRow)row.DataBoundItem).iColor);
}
}
I am trying to set the background color of each cell from the background color column. this doesn't work the color never change. Any idea of why?
我正在尝试从背景颜色列中设置每个单元格的背景颜色。这不起作用颜色永远不会改变。知道为什么吗?
I've been looking around but didn't found anything usefull
我一直在环顾四周,但没有发现任何有用的东西
采纳答案by Rémi
I finally managed to get it working. Here the code :
我终于设法让它工作。这里的代码:
private void dgvStatus_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex != color.Index)
return;
e.CellStyle.BackColor = Color.FromArgb(int.Parse(((DataRowView)dgvStatus.Rows[e.RowIndex].DataBoundItem).Row[4].ToString()));
}
if anyone know a better to do this please don't hesitate to post it. I'm open to suggestion
如果有人知道更好地做到这一点,请不要犹豫张贴。我愿意接受建议
回答by Jeb
Simply create a new DataGridViewCellStyleobject, set its back color and then assign the cell's style to it:
只需创建一个新的DataGridViewCellStyle对象,设置其背景颜色,然后将单元格的样式分配给它:
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.BackColor = Color.FromArgb(((GesTest.dsEssais.FMstatusAnomalieRow)row.DataBoundItem).iColor);
style.ForeColor = Color.Black;
row.Cells[color.Index].Style = style;
回答by Awaidus
try the following (in RowDataBound method of GridView):
尝试以下操作(在 GridView 的 RowDataBound 方法中):
protected void GridViewUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
// this will only change the rows backgound not the column header
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].BackColor = System.Drawing.Color.LightCyan; //first col
e.Row.Cells[1].BackColor = System.Drawing.Color.Black; // second col
}
}
回答by ertugrulakdag
protected void grdDataListeDetay_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[3].Text != "0")
{
for (int i = 0; i <= e.Row.Cells.Count - 1; i++)
{
e.Row.Cells[i].BackColor = System.Drawing.Color.Beige;
}
}
}
}
回答by Ysch
If you are still intrested in whythis didn't work for you at first:
如果您仍然对为什么这对您不起作用一开始感兴趣:
The reason you don't see changes you've made to the cell's style is because you do these changes beforethe form was shown, and so they are disregarded.
您看不到对单元格样式所做的更改的原因是因为您在显示表单之前进行了这些更改,因此它们被忽略。
Changing cell styles in the events suggested here will do the job, but they are called multiple times causing your style changes to happen more times than you wish, and so aren't very efficient.
在此处建议的事件中更改单元格样式可以完成这项工作,但它们会被多次调用,导致您的样式更改发生的次数比您希望的多,因此效率不高。
To solve this, either change the style after the point in your code in which the form is shown, or subscribe to the Shown event, and place your changes there (this is event is called significantly less than the other events suggested).
要解决此问题,请在代码中显示表单的点之后更改样式,或订阅 Shown 事件,并将更改放在那里(此事件的调用次数明显少于建议的其他事件)。
回答by Serkan Acuner
dataGridView1.Rows[i].Cells[7].Style.BackColor = Color.LightGreen;
回答by Serkan Acuner
int rowscount = dataGridView1.Rows.Count;
for (int i = 0; i < rowscount; i++)
{
if (!(dataGridView1.Rows[i].Cells[8].Value == null))
{
dataGridView1.Rows[i].Cells[8].Style.BackColor = Color.LightGoldenrodYellow;
}
}
回答by sailfish009
dataGridView1[row, col].Style.BackColor = System.Drawing.Color.Red;
回答by Sна?ош?а?
You can use the VisibleChangedevent handler.
您可以使用VisibleChanged事件处理程序。
private void DataGridView1_VisibleChanged(object sender, System.EventArgs e)
{
var grid = sender as DataGridView;
grid.Rows[0].Cells[0].Style.BackColor = Color.Yellow;
}
回答by Switch
Similar as shown and mentioned:
与所示和提到的类似:
Notes:Take into consideration that Cells will change their color (only)after the DataGridView Control is Visible. Therefore one practical solution would be using the:
注意:考虑到单元格将在 DataGridView 控件可见后(仅)更改其颜色。因此,一种实用的解决方案是使用:
VisibleChanged Event
VisibleChanged 事件
In case you wish to keep your style when creating new Rows; also subscribe the:
如果您希望在创建新行时保持您的风格;还订阅:
RowsAdded Event
RowsAdded 事件
Example bellow:
示例如下:
///<summary> Instantiate the DataGridView Control. </summary>
private DataGridView dgView = new DataGridView;
///<summary> Method to configure DataGridView Control. </summary>
private void DataGridView_Configuration()
{
// In this case the method just contains the VisibleChanged event subscription.
dgView.VisibleChanged += DgView_VisibleChanged;
// Uncomment line bellow in case you want to keep the style when creating new rows.
// dgView.RowsAdded += DgView_RowsAdded;
}
///<summary> The actual Method that will re-design (Paint) DataGridView Cells. </summary>
private void DataGridView_PaintCells()
{
int nrRows = dgView.Rows.Count;
int nrColumns = dgView.Columns.Count;
Color green = Color.LimeGreen;
// Iterate over the total number of Rows
for (int row = 0; row < nrRows; row++)
{
// Iterate over the total number of Columns
for (int col = 0; col < nrColumns; col++)
{
// Paint cell location (column, row)
dgView[col, row].Style.BackColor = green;
}
}
}
///<summary> The DataGridView VisibleChanged Event. </summary>
private void DataGridView_VisibleChanged(object sender, EventArgs e)
{
DataGridView_PaintCells();
}
/// <summary> Occurrs when a new Row is Created. </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
DataGridView_PaintCells();
}
Finally:Just call the DataGridView_Configuration() (method)
i.e:Form Load Event.
最后:只需调用 DataGridView_Configuration()(方法)
即:表单加载事件。

