C# 如何更改datagridview中的行颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2189376/
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
How to change row color in datagridview?
提问by EB.
I would like to change the color of a particular row in my datagridview. The row should be changed to red when the value of columncell 7 is less than the value in columncell 10. Any suggestions on how to accomplish this?
我想更改 datagridview 中特定行的颜色。当 columncell 7 的值小于 columncell 10 中的值时,该行应更改为红色。有关如何完成此操作的任何建议?
采纳答案by Ricardo Sanchez
You need to loop through the rows in the datagridview and then compare values of columns 7 and 10 on each row.
您需要遍历 datagridview 中的行,然后比较每行第 7 列和第 10 列的值。
Try this:
尝试这个:
foreach (DataGridViewRow row in vendorsDataGridView.Rows)
if (Convert.ToInt32(row.Cells[7].Value) < Convert.ToInt32(row.Cells[10].Value))
{
row.DefaultCellStyle.BackColor = Color.Red;
}
回答by SLaks
You're looking for the CellFormatting
event.
Hereis an example.
您正在寻找CellFormatting
活动。
这是一个例子。
回答by Demi
Something like the following... assuming the values in the cells are Integers.
类似于以下内容...假设单元格中的值是整数。
foreach (DataGridViewRow dgvr in myDGV.Rows)
{
if (dgvr.Cells[7].Value < dgvr.Cells[10].Value)
{
dgvr.DefaultCellStyle.ForeColor = Color.Red;
}
}
untested, so apologies for any error.
未经测试,因此对任何错误表示歉意。
If you know the particular row, you can skip the iteration:
如果您知道特定行,则可以跳过迭代:
if (myDGV.Rows[theRowIndex].Cells[7].Value < myDGV.Rows[theRowIndex].Cells[10].Value)
{
dgvr.DefaultCellStyle.ForeColor = Color.Red;
}
回答by Edison
I typically Like to use the GridView.RowDataBound Event event for this.
我通常喜欢为此使用 GridView.RowDataBound 事件事件。
protected void OrdersGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.ForeColor = System.Drawing.Color.Red;
}
}
回答by christopher
private void dtGrdVwRFIDTags_DataSourceChanged(object sender, EventArgs e)
{
dtGrdVwRFIDTags.Refresh();
this.dtGrdVwRFIDTags.Columns[1].Visible = false;
foreach (DataGridViewRow row in this.dtGrdVwRFIDTags.Rows)
{
if (row.Cells["TagStatus"].Value != null
&& row.Cells["TagStatus"].Value.ToString() == "Lost"
|| row.Cells["TagStatus"].Value != null
&& row.Cells["TagStatus"].Value.ToString() == "Damaged"
|| row.Cells["TagStatus"].Value != null
&& row.Cells["TagStatus"].Value.ToString() == "Discarded")
{
row.DefaultCellStyle.BackColor = Color.LightGray;
row.DefaultCellStyle.Font = new Font("Tahoma", 8, FontStyle.Bold);
}
else
{
row.DefaultCellStyle.BackColor = Color.Ivory;
}
}
//for (int i= 0 ; i<dtGrdVwRFIDTags.Rows.Count - 1; i++)
//{
// if (dtGrdVwRFIDTags.Rows[i].Cells[3].Value.ToString() == "Damaged")
// {
// dtGrdVwRFIDTags.Rows[i].Cells["TagStatus"].Style.BackColor = Color.Red;
// }
//}
}
回答by Eden
I was just investigating this issue (so I know this question was published almost 3 years ago, but maybe it will help someone... ) but it seems that a better option is to place the code inside the RowPrePaint
event so that you don't have to traverse every row, only those that get painted (so it will perform much better on large amount of data:
我只是在调查这个问题(所以我知道这个问题是在大约 3 年前发布的,但也许它会帮助某人......)但似乎更好的选择是将代码放在RowPrePaint
事件中,这样你就不会必须遍历每一行,只有那些被绘制的行(所以它会在大量数据上表现得更好:
Attach to the event
附加到事件
this.dataGridView1.RowPrePaint
+= new System.Windows.Forms.DataGridViewRowPrePaintEventHandler(
this.dataGridView1_RowPrePaint);
The event code
事件代码
private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
if (Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[7].Text) < Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[10].Text))
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Beige;
}
}
回答by user1614017
I had trouble changing the text color as well - I never saw the color change.
我也无法更改文本颜色 - 我从未看到颜色变化。
Until I added the code to change the text color to the event DataBindingsComplete
for DataGridView
. After that it worked.
直到我添加了将文本颜色更改DataBindingsComplete
为DataGridView
. 在那之后它起作用了。
I hope this will help people who face the same problem.
我希望这会帮助面临同样问题的人。
回答by Denise Skidmore
Some people like to use the Paint
, CellPainting
or CellFormatting
events, but note that changing a style in these events causes recursive calls. If you use DataBindingComplete
it will execute only once. The argument for CellFormatting
is that it is called only on visible cells, so you don't have to format non-visible cells, but you format them multiple times.
有些人喜欢使用Paint
,CellPainting
或CellFormatting
事件,但请注意,更改这些事件中的样式会导致递归调用。如果你使用DataBindingComplete
它只会执行一次。的论点CellFormatting
是它仅在可见单元格上调用,因此您不必格式化不可见单元格,但可以多次格式化它们。
回答by Pratik 1020
You can Change Backcolor
row by row using your condition.and this function call after applying Datasource
of DatagridView
.
你可以改变Backcolor
使用condition.and调用这个函数应用后逐行Datasource
的DatagridView
。
Here Is the function for that.
Simply copy that and put it after Databind
这是功能。简单地复制它并把它放在后面Databind
private void ChangeRowColor()
{
for (int i = 0; i < gvItem.Rows.Count; i++)
{
if (BindList[i].MainID == 0 && !BindList[i].SchemeID.HasValue)
gvItem.Rows[i].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#C9CADD");
else if (BindList[i].MainID > 0 && !BindList[i].SchemeID.HasValue)
gvItem.Rows[i].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#DDC9C9");
else if (BindList[i].MainID > 0)
gvItem.Rows[i].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#D5E8D7");
else
gvItem.Rows[i].DefaultCellStyle.BackColor = Color.White;
}
}
回答by Levi
Works on Visual Studio 2010. (I tried it and it works!) It will paint your entire row.
适用于 Visual Studio 2010。(我试过了,效果很好!) 它将绘制您的整行。
- Create a button for the
datagridview
. - Create a
CellClick
event and put the next line of code inside of it.
- 为
datagridview
.创建一个按钮。 - 创建一个
CellClick
事件并将下一行代码放入其中。
if (dataGridView3.Columns[e.ColumnIndex].Index.Equals(0)
{
dataGridView3.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Beige;
}