如何更改datagridview的行颜色,其中单元格值是c#中的某个值 [英] how to change row colour of datagridview where cell value is some value in c#
问题描述
我正在使用win-apps,用c#编码。
我有一个datagridview,它从microsoft sql server数据库加载它的数据。我的需要是我想在datagridview单元格值小于35的位置设置行颜色。
我的想法如下所示。
I am working with win-apps,coding in c#.
I have a datagridview which is loading its data from microsoft sql server database. My need is I want to set row color where the datagridview cell value is less than 35..
My idea is as given below.
//this is just my Idea,Not correct code,so please add code
private void colorchange()
{
if (dataGridView4.cellvaue <= 35)
{
dataGridView4.row_fore_colour = red;
}
}
datagridview单元格值是文本格式。所以请不要忘记转换文本为整数值,然后给我一个解决我的问题的方法(我不知道datagridview单元格值的转换)
The datagridview cell values are in text format.So please don't forget to convert the text to integer values,and then give me a solution to my problem(i don't know conversion of datagridview cell values)
推荐答案
首先你必须订阅DataGrid像这样的CellFormatting事件:
First you must subscribe to DataGrid CellFormatting event like this:
dataGrid1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGrid1_CellFormatting);
然后在事件处理程序中执行以下操作:
And then in the event handler do something like this:
void dataGrid1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (this.dataGrid1.Rows[e.RowIndex].Cells["cellToCheck"].Value != null)
{
if ((int)this.dataGrid1.Rows[e.RowIndex].Cells["cellToCheck"].Value <=35)
{
this.dataGrid1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
}
}
}
希望有帮助
Hope that helps
这篇关于如何更改datagridview的行颜色,其中单元格值是c#中的某个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!