?
Solved

C# ASP.NET, datagrid row color

Posted on 2006-03-28
6
Medium Priority
?
498 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!
0
Comment
Question by:vandy02
6 Comments
 
LVL 10

Expert Comment

by:SystemExpert
ID: 16318596
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
 
LVL 4

Assisted Solution

by:avanishp
avanishp earned 800 total points
ID: 16318599
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
 
LVL 15

Accepted Solution

by:
GavinMannion earned 1200 total points
ID: 16318656
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:vandy02
ID: 16318790
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
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16318807
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
 

Author Comment

by:vandy02
ID: 16318819
That is it!!!!

Thank you all!!!  I have been working on this for awhile.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question