Highlight row according to 2nd column value using ASP.NET datagrid

Posted on 2009-04-02
Last Modified: 2013-11-08
I want to change the background colour of rows on a datagrid depending on the value of the 2nd cell in each row.

I've seen a lot of examples of subroutines but I can't see how they these subroutines are called so as to update the datagrid.  My datagrid has id="dgrdStock".  How can I:
1) get a subroutine to set the colour of rows depending on cell values and
2) apply it to my datagrid?

The code below is lifted and modified from other solutions I've seen - but untested as I don;t know how to call this subroutine.
Sub dgrdStock_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgrdStock.CellPainting

	Select Case dgrdStock.Rows(e.RowIndex).Cells.Item(2).Value

		Case "Available UK"

			dgrdStock.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Green

		Case "Available Europe"

			dgrdStock.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Blue

		Case Else

			dgrdStock.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Red

		End Select

End Sub

Open in new window

Question by:Beamson
  • 4
  • 3
LVL 20

Expert Comment

ID: 24048354
Are you using ASP.Net? or is it a windows forms application.

As far as I know there's no Cell Painting event in datagrid or gridview.

Author Comment

ID: 24048403
Its for an ASP.NET/ VB web page written using a text editor. ASP.NET/ VB.

The code I've cobbled together has been taken from the numerous sample codes I've got on other posts (which then don't explain how/where the subs are called).  You can probably see what i'm trying to achieve but I need some help getting it right.
LVL 20

Expert Comment

ID: 24048451

Well this won't work for u.

which version of are u using? 2.0 and higher?
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Author Comment

ID: 24048479
2.0 and higher - yes. (I believe the latest 3.5 framework is installed on the server)
LVL 20

Assisted Solution

informaniac earned 50 total points
ID: 24048553
Well I suggest u use the gridview control instead of the datagrid control.

Then handle the rowdatabound event of the gridview control.

To handle the rowdatabound of gridview, go to the properties of the gridview and click on the events icon. (thunder)

u 'll see a rowdatabound event. Double click on that..

write this code in that event


 If e.Row.RowType = DataControlRowType.DataRow Then

     If e.Row.Cells(2) = "Y" Then

         e.Row.Cells(5).BackColor = System.Drawing.Color.Red

     End If

 End If

Open in new window


Author Comment

ID: 24048594
I'm writing in a text editor (well, Dreamweaver)

My problem is not with the logic but with getting the coding right.
The existing code has:
a single datagrid
a single button
a single subroutine which fires on the button's onclick event which retrieves the records and binds them to the datagrid.

What code do I need to need to add (and where) to solve the row colouring effect?

Accepted Solution

Beamson earned 0 total points
ID: 24278374
In the end, I had to abandon this line of question and do without cell colouring.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

25 Experts available now in Live!

Get 1:1 Help Now