Solved

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

Posted on 2009-04-02
7
562 Views
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

0
Comment
Question by:Beamson
  • 4
  • 3
7 Comments
 
LVL 20

Expert Comment

by:informaniac
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 asp.net datagrid or gridview.
0
 
LVL 1

Author Comment

by:Beamson
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.
0
 
LVL 20

Expert Comment

by:informaniac
ID: 24048451
Ok.

Well this won't work for u.

which version of asp.net are u using? 2.0 and higher?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

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

Assisted Solution

by:informaniac
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

0
 
LVL 1

Author Comment

by:Beamson
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?
0
 
LVL 1

Accepted Solution

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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 antispam), the adminiā€¦

733 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