Highlighting rows in a DataGrid

Posted on 2005-04-29
Last Modified: 2010-04-17
How can I highlight certain rows in a DataGrid in Visual Basic .NET 2003?

Question by:SETP
    LVL 44

    Expert Comment

    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
    LVL 2

    Expert Comment

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

    Accepted Solution

    Windows or Web ?

    the 2 previous comments apply to Web datagrid.

    If you meant Windows, have a look at

    Author Comment

    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!
    LVL 69

    Expert Comment

    by:Éric Moreau
    The DataGrid has never been the best Microsoft control!
    LVL 7

    Expert Comment

    LVL 69

    Expert Comment

    by:Éric Moreau
    recommandation: points to emoreau

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    This article will show, step by step, how to integrate R code into a R Sweave document
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now