We help IT Professionals succeed at work.

C# ASP.NET, datagrid row color

vandy02
vandy02 asked
on
Medium Priority
512 Views
Last Modified: 2012-06-27
I am wanting to look at values of a column through a datagrid and look at a particular field like Status and if the value is 'Y' I want to color the row green and if it is 'N' I want to color the row red.   In most cases there would be multiple rows so multiple rows of green and red.  

I hope this makes since.

Thanks!
Comment
Watch Question

hi,

In ItemDataBound event you can use like this

if (e.Item.Cells[0].Text = "Y")
{
                                                            e.Item.Cells[0].Text = "";
                e.Item.Cells[0].BackColor = System.Drawing.Color.Green ;
}
else
{
      e.Item.Cells[0].Text = "";
      e.Item.Cells[0].BackColor = System.Drawing.Color.Red  ;
}

Reference Link

http://www.codeproject.com/aspnet/PrettyDatagrids.asp


Thanks

Thanks
Commented:
private void CustomGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
if (e.Item.cell[1].Text=='Y')
 e.Item.BackColor=#Yourcolorcode
else
 e.Item.BackColor=#YourOthercolorcode
}
}

Use this itembound event for your datagrid

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
You need to do this in the OnItem_DataBound event of the datagrid.

something along the lines of

string mycheck = MyDataGrid.Rows[e.RowIndex].Cells[4].ToString()

if( myCheck == 'N' )
MyDataGrid.Rows[e.RowIndex].Style.BackColor = 'Red'

That code won't run but should give you the idea. If you are still lost then by all means post back and let me know what language your are coding in and I will post some more usefull code.

Author

Commented:
I think my 'if' statement is not working with respect to comparing strings.  Within the first 'if' I search for "CA" of which is within the datagrid as my output for the columns are CA and TX.  So this populates a listbox with 'k' if it is ever a true..howver it never populates the list box with 'k' -- only the values of CA and TX.  Same result with the second if...so it must be something within the if..

Here is what I have:

if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)    
{
if (e.Item.Cells[5].Text.ToString() =="CA")
{
e.Item.BackColor= Color.Aqua;
ListBox1.Items.Add("k");
}
else
{
//e.Item.BackColor= Color.Red;
ListBox1.Items.Add(e.Item.Cells[5].Text);
}

string myCheck = e.Item.Cells[5].Text.ToString();

if( myCheck == "CA" )
{
e..Item.BackColor= Color.Gainsboro;
}

Thanks!
I can only guess that your value is not exactly "CA"

It could be " CA" or "ca" or something along those lines.

Try

if (e.Item.Cells[5].Text.ToString().ToUpper().Trim() =="CA")

Author

Commented:
That is it!!!!

Thank you all!!!  I have been working on this for awhile.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.