Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
C#
//this is just my Idea,Not correct code,so please add code  
 private void colorchange()
    {
        if (dataGridView4.cellvaue <= 35)
        {
            dataGridView4.row_fore_colour = red;
        }
    }  


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)
Posted

1 solution

First you must subscribe to DataGrid CellFormatting event like this:

C#
dataGrid1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGrid1_CellFormatting);


And then in the event handler do something like this:

C#
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
 
Share this answer
 
Comments
n shiva Ram 21-Oct-14 4:25am    
What should I write at e.rowIndex
Pikoh 21-Oct-14 4:29am    
CellFormatting event is called for each row in the datagrid. e.RowIndex contains the row number that is being evaluated in that call. You don't have to write nothing in there,just read it to know which row to paint.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900