?
Solved

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

Posted on 2009-04-02
7
Medium Priority
?
576 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 200 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

Industry Leaders: 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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

741 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