Shiva Debrown
Shiva Debrown

Reputation: 177

how to change row colour of datagridview where cell value is some value in c#

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;
        }
    }  

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)

Upvotes: 0

Views: 1170

Answers (4)

Lemon Kazi
Lemon Kazi

Reputation: 3311

You can use cell number in your condition. I solved my issue using cell number then fetch cell value with if condition. You can use this code.

void dataGridView4_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
for (int i = 0; i < dataGridView4.Rows.Count; i++)
    {
        if (e.ColumnIndex == 9)
        {
            if (dataGridView2.CurrentCell != null && dataGridView2.CurrentCell.Value != null && dataGridView2.CurrentCell.Value <=35)
            {
            dataGridView4.Rows[i].DefaultCellStyle.BackColor = Color.Red;
            }
        }
    }
}

Upvotes: 0

I A Khan
I A Khan

Reputation: 8841

try this may be work

void dataGridView4_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
for (int i = 0; i < dataGridView4.Rows.Count; i++)
            {
        if (dataGridView4.Rows[i].Cells["cellname"].Value != null)
        {
            if ((int)dataGridView4.Rows[i].Cells["cellname"].Value <=35)
            {
                dataGridView4.Rows[i].DefaultCellStyle.BackColor = Color.Red;
            }
        }
    }
}

this will check only one column "cellname", if you want to check all columns then add one extra for loop ..

Upvotes: 0

Koen
Koen

Reputation: 644

Think this might be the way to do it:

    for (int i = 0; i < dataGridView4.Rows.Count; i++)
            {
                for (int o = 0; o < dataGridView4.Rows[i].Cells.Count;o++ )
                {  
                     int num1;
                     if(dataGridView4.Rows[i].Cells[o].Value != null)
                     {
                       string text1 = dataGridView4.Rows[i].Cells[o].Value.toString;
                       bool res = int.TryParse(text1, out num1);
                       if (res == true)
                        {
                          if(Convert.ToInt32(dataGridView4.Rows[i].Cells[o].Value) < 35)
                         {
                          dataGridView4.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Red; 
                         }
                        }
                      }

                }
            }

Upvotes: 0

Shiva Debrown
Shiva Debrown

Reputation: 177

void dataGridView4_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (this.dataGridView4.Rows[e.RowIndex].Cells["cellToCheck"].Value != null)
        {
            if ((int)this.dataGridView4.Rows[e.RowIndex].Cells["cellToCheck"].Value <=35)
            {
                this.dataGridView4.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
            }

        }
    }

I found this in code project which I wanted..

Upvotes: 1

Related Questions