Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
|
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 22nd, 2009, 12:28 AM
Registered User
 
Join Date: Jun 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Formatting the GridView Rows Based on the Underlying Data

Per Microsoft's Dev Article: http://msdn.microsoft.com/en-us/library/aa479342.aspx

The code below works however; I need to change the color of the row based on textual values and not integers.

For example, currently if the value in the Archive column is set to 0 then the row's background color is formatted to yellow. I need to modify this code so that I can use text values instead such as yes or no and not integers.


Protected Sub GridView1_RowDataBound1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
'determine the value of the Archive field
Dim Archive As Integer = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Archive"))
If Archive = 0 Then
'color the background of the row yellow
e.Row.BackColor = Drawing.Color.Yellow
End If
End If
End Sub
End Class
 
Old April 22nd, 2009, 07:40 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hello...

You have the color name and want to pass it to the backcolor property?? is that what you want???
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old April 22nd, 2009, 11:08 AM
Registered User
 
Join Date: Jun 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Formatting the GridView Rows Based on the Underlying Data

Hi gbianchi,

Thank you for your response.

I want to color the rows yellow if the value of the Archive field equals YES and then I would like to color the rows blue if the Archive field equals a value of NO.

My Archive field is column number 8 in my gridview.

The code I pasted below was an example provided by the MSDN but it uses integers instead of Text to color the rows based on value in the gridview. It is working fine but now I just need to figure out how to modify that code so that I can use the text value of YES and NO to color the rows.

Thanks!
 
Old April 22nd, 2009, 12:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

OK, I think I got it. The code is just the same, the only things you have to change:
1- convert.toint32 should be taken off (eval returns a string)
2- archiver should be of type string
3- in the if, compare the value with "YES"

I believe that all you need...
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old April 23rd, 2009, 09:19 PM
Registered User
 
Join Date: Jun 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Formatting the GridView Rows Based on the Underlying Data

Thank you GBianchi,

I ended up using the following code:

Code:
 
Protected
Code:
Sub GridView1_RowDataBound1(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
If ((e.Row.Cells(9).Text.Trim = "YES") Or (e.Row.Cells(9).Text.Trim = "Yes") _
Or (e.Row.Cells(9).Text.Trim = "yes")) Then
e.Row.BackColor = Drawing.Color.LightSalmon
ElseIf ((e.Row.Cells(9).Text.Trim = "NO") Or (e.Row.Cells(9).Text.Trim = "No") _
Or (e.Row.Cells(9).Text.Trim = "no")) Then
e.Row.BackColor = Drawing.Color.White
Else
e.Row.BackColor = Drawing.Color.White
EndIf
EndIf
EndSub

 
Old April 23rd, 2009, 10:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Lightbulb

I have a better version for you:

Code:
 Protected Sub GridView1_RowDataBound1(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
If ((String.compare(e.Row.Cells(9).Text.trim,"YES",StringComparison.OrdinalIgnoreCase) = 0 Then
e.Row.BackColor = Drawing.Color.LightSalmon
Else
e.Row.BackColor = Drawing.Color.White
EndIf
endif End Sub
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old April 24th, 2009, 04:27 PM
Registered User
 
Join Date: Jun 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up Formatting the GridView Rows Based on the Underlying Data

Thank you Gibianchi,

That is certainly much more efficient!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Get GridView Cell Value Based on GridView Column stublair C# 2008 aka C# 3.0 0 September 4th, 2008 08:30 AM
Best Practice for extra data under GridView rows SeanKilleen ASP.NET 2.0 Basics 0 July 19th, 2007 12:51 PM
Display subreport with no underlying data bph Access 4 April 11th, 2005 04:08 PM
Formatting sql query rows as columns with stack sastian PHP Databases 0 March 25th, 2005 04:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.