• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

C# ASP.NET, datagrid row color

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!
0
vandy02
Asked:
vandy02
2 Solutions
 
SystemExpertCommented:
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
0
 
avanishpCommented:
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
0
 
GavinMannionCommented:
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.

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
vandy02Author 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!
0
 
GavinMannionCommented:
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")
0
 
vandy02Author Commented:
That is it!!!!

Thank you all!!!  I have been working on this for awhile.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now