Solved

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

Posted on 2009-04-02
7
549 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
Comment Utility
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
Comment Utility
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
Comment Utility
Ok.

Well this won't work for u.

which version of asp.net are u using? 2.0 and higher?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:Beamson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
In the end, I had to abandon this line of question and do without cell colouring.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual Studio installation 5 32
.NET Error 7 41
How do I get the id from URL? 19 46
Showdialog 8 20
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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

12 Experts available now in Live!

Get 1:1 Help Now