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!
vandy02Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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

Thank you all!!!  I have been working on this for awhile.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.