Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Have a DataGridView in which I have a column named 'Status'.

If Value in the Status field is "Sls" then the BackColor of the row should become blue. I have used a foreach loop for changing the BackColor of the Row but problem is that the user can add, edit, delete the rows in DataGridView and if new row is added in the DataGridView with value of the status field = "Sls" then BackColor of any of the row doesn't changes.

Please help.

Is there Some event of DataGridView which will automatically check the value of the particular column and will change the color of the row accordingly?

MY Code is like this:

C#
private void dgInvoiceMn_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
            if (dgInvoiceMn.Rows[e.RowIndex].Cells["ExtDate"].Value.ToString().Trim() != string.Empty)
      {
        dgInvoiceMn.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.MediumPurple;
      }
      if (dgInvoiceMn.Rows[e.RowIndex].Cells["Feeder"].Value.ToString().Trim().ToUpper() == "Y")
      {
        dgInvoiceMn.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Khaki;
      }
      if (dgInvoiceMn.Rows[e.RowIndex].Cells["RtnFlg"].Value.ToString().Trim() == "C")
      {
        dgInvoiceMn.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
      }
      if (dgInvoiceMn.Rows[e.RowIndex].Cells["RtnFlg"].Value.ToString().Trim() == "R")
      {
        dgInvoiceMn.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.HotPink;
      }
         }
Posted
Updated 12-Dec-10 19:26pm
v5
Comments
Abdul Quader Mamun 11-Dec-10 6:03am    
Spelling check.
OriginalGriff 11-Dec-10 6:04am    
Change your user ID - Never post your email address in any forum, unless you really like spam!
TweakBird 11-Dec-10 8:46am    
can you provide sample code, in which event your were setting back color?

You could possibly handle the CellFormatting event, something like this:
C#
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == "Status") //Only do it for 'Status' column
    {
      if (e.Value != null && e.Value.ToString().ToUpper() == "SLS")  // if not null and = 'Sls' converted to upper to allow for typos
      {
        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
      }
      else  // not 'sls' so use default
      {
        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = dataGridView1.DefaultCellStyle.BackColor;
      }
    }
}


I have not tested this but it should work
 
Share this answer
 
Comments
Jayesh Deriya 13-Dec-10 1:17am    
Thanks Henry Minute.
I have used cellformatting event but it seems ther is some problem with it.If the column is visible in the datagridview than only it changes color.In My Case, i m not showing the column Status to User in datagridview.i have set the column Visible property to false.So in this case what can i do? please help.

My Code is Like this:

private void dgInvoiceMn_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dgInvoiceMn.Rows[e.RowIndex].Cells["ExtDate"].Value.ToString().Trim() != string.Empty)
{
dgInvoiceMn.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.MediumPurple;
}
if (dgInvoiceMn.Rows[e.RowIndex].Cells["Feeder"].Value.ToString().Trim().ToUpper() == "Y")
{
dgInvoiceMn.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Khaki;
}
if (dgInvoiceMn.Rows[e.RowIndex].Cells["RtnFlg"].Value.ToString().Trim() == "C")
{
dgInvoiceMn.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
}
if (dgInvoiceMn.Rows[e.RowIndex].Cells["RtnFlg"].Value.ToString().Trim() == "R")
{
dgInvoiceMn.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.HotPink;
}
}

Jayesh Deriya 13-Dec-10 1:22am    
Please help buddy.Its Urgent
I can only suggest that you investigate the DataGridView.InvalidateRow()[^] method and apply it within your CellFormatting handler.

No guarantees, as once again I have not tested it although from the description it should force a repaint of the Row. Be careful though as it might also re-trigger the CellFormatting which will trigger the InvalidateRow etc....... and you will get a stackoverflow exception.

Hope this helps. :)
 
Share this answer
 
Comments
Henry Minute 12-Feb-11 15:00pm    
Is there a reason that you keep posting links to me with no comments?

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