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

April 22nd, 2009, 12:28 AM
|
|
Registered User
|
|
Join Date: Jun 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

April 22nd, 2009, 07:40 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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.
================================================== =========
|
|

April 22nd, 2009, 11:08 AM
|
|
Registered User
|
|
Join Date: Jun 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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!
|
|

April 22nd, 2009, 12:49 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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.
================================================== =========
|
|

April 23rd, 2009, 09:19 PM
|
|
Registered User
|
|
Join Date: Jun 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Formatting the GridView Rows Based on the Underlying Data
Thank you GBianchi,
I ended up using the following code:
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

|
|

April 23rd, 2009, 10:14 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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 Elsee.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.
================================================== =========
|
|

April 24th, 2009, 04:27 PM
|
|
Registered User
|
|
Join Date: Jun 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Formatting the GridView Rows Based on the Underlying Data
Thank you Gibianchi,
That is certainly much more efficient!
|
|
 |