Solved

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

Posted on 2009-04-02
7
561 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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