Solved

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

Posted on 2009-04-02
7
569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

 
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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

695 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