Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid and color question... 1

Status
Not open for further replies.

Junior1544

Technical User
Apr 20, 2001
1,267
US
Does any one know how I would go about using a condition in a display'd record within a datagrid to change the color of the current row??

What i want to do is if a date in a record is older then today (the record has expired), to make the whole row (or even just part of it if that's easier) change to a different color back ground... say red...

Any help, pointers, or direction and I will be gratefull...

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Here is what I have done in the past.

In a function that gets called when the OnItemDataBound event of the databound grid gets fired, I check to make sure the object causing the event to be fired is not a Header, Separator or a Footer. If it isn’t, do your magic.

First compare the current date with the one from the recordset. If it is earlier than today, you set the background color to red

Code here is in C# but you can easily change it to VB if you wish.

In the .aspx file:
Code:
<asp:DataGrid …Attributes… OnItemDataBound=”OnItemDataBoundEventHandler”> //This specifies the function to call when populating the grid.

In the .aspx.cs (or .vb) file:
Code:
public void OnItemDataBoundEventHandler(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    ListItemType itemType = (ListItemType) e.Item.ItemType;
    If (itemType == ListItemType.Separator || itemType == ListItemType.Header || itemType == ListItemType.Footer) return;  //Don’t want to do anything here

    //Code here to check if the database date is before today.
    //If it is, execute the following line of code
    e.Item.BackColor = Color.FromName(“Red”);
}
Thanks,

Gabe
 
Thank you, I didn't know about the
OnItemDataBound=”OnItemDataBoundEventHandler”
event... Now i got some thing to work on:) Thank you!
--James junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Thank you for the star.

I learned about this in the &quot;Programming ASP.NET&quot; by O'Reilly Chapter 3.

One note:
OnItemDataBoundEventHandler can be called anything you want.

Code:
OnItemDataBound=”OnItemDataBoundEventHandler”
can be written as
Code:
OnItemDataBound=”MyDBGItemEventHandler”

Just make sure the function name in your code behind file is the same.
Thanks,

Gabe
 
Yea, I got that part.. Thanks for the time... I started programming asp in vs about a week ago... I've read through the book asp.net for dummies... is there a better book you would suggest?? the for dummies book does every thing in spegetti code and it's hard to find the way to do it in vs some times... took me a day to figure out how to do includes because they are done differently...

Thanks for all the help...

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
I have been developing in ASP and VB for many years and have also just started the transition to the .Net platform. I am lucky enough to be employed as a programmer so my company purchases the books I need/want. I also decided to make the switch from VB to C# for several reasons. One, because VB 6.0 and VB.Net are so different. I would try to think like VB 6.0 when developing in VB.Net and it just confused me. Since C# is totally new to me, I don’t have that development history to get me mixed up. Second, C# is very similar in syntax to Java (which I use) and C++ (which I don’t use). I figure if I am going to learn a new language, I should learn one that is similar to other popular languages. Third, it sounds cooler to say I develop in C# than VB.Net. Of course this is my personal opinion. I also think that C# is easier to learn than VB.Net. Also my personal opinion.

With that said, I obtained the “Programming ASP.NET” and “Programming C#” books by O’Reilly. I really like them both. The ASP.NET book has almost every example in both C# and VB. That is nice for two reasons. One, the examples are in the language you are working with. Two, you can learn the other language this way. If you know VB, you can see how to do it in C#. ASP.NET pretty much assumes you are going to be using VS.Net (which I use) so that is very nice. The book uses the code behind files for most coding, which is very nice since that is how you are going to be developing anyway.

One complaint I do have is that neither book gets very deep into ADO.NET. I guess I need another book for that. I know most information can be found on the web but sometimes its nice to have a book to flip through.

I hope this helps you.

Thanks,

Gabe
 
it does... I have asp in a nutshell by O'Reilly... Havn't read it yet because I want to use asp.net... I got this as a backup if i have to use asp... I'll look into the ASP.net O'Reilly book...

I'm using vb.net for programming since i've been programming in vba (access databases) for a few months and am pretty good at it...

Thank you so much for all the help... I wish I could give you another star...

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top