C#特定行Datagridview更改颜色 [英] C# Specific row Datagridview Change color
本文介绍了C#特定行Datagridview更改颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,如何更改包含已售出的特定行的颜色
这是我的代码
string a = dataGridView1.Rows [ 0 ] .Cells [ 2 ]。Value.ToString();
foreach (DataGridViewRow Myrow in dataGridView1.Rows)
{
if (a == Luzon ) // ))// Columns.Contains(SOLD))// Columns.Contains(SOLD ))
{
dataGridView1.DefaultCellStyle.BackColor = Color.Red;
}
}
使用此代码后,所有行都变为红色,我想要的是更改具有单词SOLD的行的颜色。请帮助
解决方案
目前您正在将DataGridView的颜色设置为红色。
如果您只想要一个特定的行,则必须设置这一行的颜色。
就像这样:
myrow.defaultcellstyle.forecolor = Color.Red;
BTW。只是为了改善你的代码,为什么你设置一个循环外?
这样会更好我猜
string a = dataGridView1.Rows [ 0 ].Cells [ 2 ]。Value.ToString();
foreach (DataGridViewRow Myrow in dataGridView1.Rows)
{
if ( Myrow.Cells [ 2 ]。Value.ToString()== Luzon) // ))// Columns.Contains(SOLD)) //Columns.Contains(\"SOLD))
{
Myrow.DefaultCellStyle.BackColor = Color.Red;
}
}
更好的是使用Columns名称而不是2中的这个:)
like Myrow.Cells [ NameOfColumn]。值
Hello, How can i change the color of the specific rows that contains "SOLD"
Here's my code
string a = dataGridView1.Rows[0].Cells[2].Value.ToString();
foreach (DataGridViewRow Myrow in dataGridView1.Rows)
{
if (a == "Luzon")//"))//Columns.Contains("SOLD"))//Columns.Contains("SOLD"))
{
dataGridView1.DefaultCellStyle.BackColor = Color.Red;
}
}
After doing using this code, all rows turns red, What i want is to change the color of the rows that has the word "SOLD" . Please help
解决方案
Currently you are setting the color for the DataGridView to red.
If you just want a specific row you have to set the color for this row.
Just like this:
myrow.defaultcellstyle.forecolor="Color.Red";
BTW. just to improve your code a bit, why do you set a outside of the loop?
This would be better i guess
string a = dataGridView1.Rows[0].Cells[2].Value.ToString();foreach (DataGridViewRow Myrow in dataGridView1.Rows) { if (Myrow.Cells[2].Value.ToString() == "Luzon")//"))//Columns.Contains("SOLD"))//Columns.Contains("SOLD")) { Myrow.DefaultCellStyle.BackColor = Color.Red; } }
Even better would be to use to Columns name instead of the 2 in this :)
like Myrow.Cells["NameOfColumn"].Value
这篇关于C#特定行Datagridview更改颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文