Link to home
Start Free TrialLog in
Avatar of SETP
SETP

asked on

Highlighting rows in a DataGrid

How can I highlight certain rows in a DataGrid in Visual Basic .NET 2003?

Thanks
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi SETP,

In the ItemDataBound event of the grid, you can test whether the row should be highlighted and alter the backcolor and/or forecolor of the cells as appropriate.

Tim Cottee
Avatar of jong3
jong3

A DataGrid is basically a dynamically created table.  If you look at the code once your page is generated, you will see a bunch of table tags (TD, TR, etc..)

In order to set the color of the row in a table, you need to do it before the code is generated.  This can be done in the "ItemDataBound" event of the datagrid.

Inside your ItemDataBound event, add something like the following:

   If e.Item.Cells(0).Text = MYCONDITION Then
         e.Item.BackColor = System.Drawing.Color.Red   //sets the bg color
         e.Item.Font.Bold = True                                    //sets the font to bold
   End If

I hope this helps.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SETP

ASKER

Sorry - I'm using Windows Forms, not Web Forms.

I check out that link. Is there no simpler way of doing it? I can't believe Microsoft didn't add a simple way to change the background color of a row!
The DataGrid has never been the best Microsoft control!
recommandation: points to emoreau