Click here to Skip to main content
14,735,478 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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;
        }
    }
   
Comments
Velrats 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 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 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 16-Apr-13 23:58pm
   
Thanks a lot @OriginalGriff... this worked out to me as expected :)
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..
   
Comments
Velrats 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... :(
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;
                    }
                }
            }
        }
   
v2
Comments
Velrats 17-Apr-13 4:29am
   
Thanks a lot..! :)
Thomas Barbare 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)




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