Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows Forms WinForm
Hi All,
 
I stuck with a trouble... please help me
 
I have a data grid view in my user control, in which all the fields are textboxes.
 
I need to show the green color for the particular cell, if the value in the cell is more than 75.
 
    for (int rowsCount = 0; rowsCount < dgvReports.RowCount; rowsCount++)
            {
                for (int ColsCount = 0; ColsCount < dgvReports.ColumnCount; ColsCount++)
                {
                  int DataFromGrid = Convert.ToInt16(dgvReports.Rows[rowsCount].Cells[ColsCount].Value);
                    if (DataValueFromGrid > 75)
                    {
          //after this there is no property for setting back ground color for that particular cell
                        dgvReports.Rows[rowsCount].Cells[ColsCount] 
                    }
                }
            }
Posted 16-Apr-13 6:36am
Velrats1.3K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Yes, there is - it's part of the cell Style property:
        private void myDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
            {
            DataGridView dgv = sender as DataGridView;
            if (dgv != null && e.RowIndex >= 0)
                {
                DataGridViewCell cell =  dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
                cell.Style.BackColor = Color.Red;
                }
            }
  Permalink  
Comments
Velrats at 16-Apr-13 12:11pm
   
is this can be done only on cell click.... I want the colors to be changed on load of the User control....
Velrats at 16-Apr-13 12:21pm
   
int DataFromGrid = Convert.ToInt16(dgvReports.Rows[rowsCount].Cells[ColsCount].Value);
 
if (DataFromGrid > 75)
{
DataGridViewCell cell = dgvReports.Rows[rowsCount].Cells[ColsCount];
cell.Style.BackColor = Color.Red;
}
 
I tried like this... but dint work...
OriginalGriff at 16-Apr-13 12:37pm
   
You can't do it in the Load event - that happens well before the UI thread has loaded any data into the actual DGV.
 
Have you considered doing it in the CellPainting event?
private void myDataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv != null && e.RowIndex >= 0 && dgv.Rows[e.RowIndex].Cells.Count > 0)
{
DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[3];
if ((int)cell.Value > 10)
{
cell.Style.BackColor = Color.Red;
}
}
}
Velrats at 16-Apr-13 23:58pm
   
Thanks a lot @OriginalGriff... this worked out to me as expected :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Write the code in RowDataBound event of gridview:
 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
            if(Convert.ToInt32(e.Row.Cells[0].Text) > 75) 
                  e.Row.Cells[0].BackColor = Color.Green;
      }
}
 
Hope this will help..
  Permalink  
Comments
Velrats at 16-Apr-13 12:23pm
   
I think RowDataBound event is there only in Web Forms.... I'm using Windows Forms.... i couldn't find that event in the events list... :(
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You can bind at RowsAdded event.
 
void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            foreach (DataGridViewCell item in dataGridView1.Rows[e.RowIndex].Cells)
            {
                if (item.Value.GetType() == typeof(int))
                {
                    if ((int)item.Value > 50)
                    {
                        item.Style.BackColor = Color.Red;
                    }
                }
            }
        }
 
You can also do that for the CellEndEdit event if you want to check everytime user edit a cell.
 
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            foreach (DataGridViewCell item in dataGridView1.Rows[e.RowIndex].Cells)
            {
                int value;
                if (int.TryParse((string)item.Value,out value))
                {
                    if (value > 50)
                    {
                        item.Style.BackColor = Color.Red;
                    }
                    else
                    {
                        item.Style.BackColor = Color.Green;
                    }
                }
            }
        }
  Permalink  
v2
Comments
Velrats at 17-Apr-13 4:29am
   
Thanks a lot..! :)
Thomas Barbare at 17-Apr-13 4:48am
   
You're welcome.

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 17 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100