Before you go, oh no not another one, I have searched and searched for this and some come close but not exactly addressing what is going on.
I have a Gridview and the 4th column of this control is a template field. What I am trying to do is color this cell based on the text that the label has in it. The code below works but I am noticing now that it only works on the first row and not the next
row. The code looks for a label if the RowState is normal and a different one if the RowState is edit. What am I missing?
Protected Sub gv_Writeups_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gv_Writeups.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState = DataControlRowState.Normal Then
Dim lblOccurrenceTitle As Label = TryCast(e.Row.FindControl("lbl_Item_Decp"), Label)
Dim OccurrenceTitle As String = lblOccurrenceTitle.Text
If OccurrenceTitle.Contains("Final") Then
e.Row.Cells(3).BackColor = System.Drawing.Color.Red
e.Row.Cells(3).ForeColor = System.Drawing.Color.White
ElseIf e.Row.Cells(3).Text.Contains("Verbal") Then
e.Row.Cells(3).BackColor = System.Drawing.Color.Yellow
e.Row.Cells(3).ForeColor = System.Drawing.Color.Black
Else
e.Row.Cells(3).BackColor = System.Drawing.Color.Orange
e.Row.Cells(3).ForeColor = System.Drawing.Color.White
End If
End If
If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState = DataControlRowState.Edit Then
Dim lblOccurrenceTitle As Label = TryCast(e.Row.FindControl("lbl_Edit_Decp"), Label)
Dim OccurrenceTitle As String = lblOccurrenceTitle.Text
If OccurrenceTitle.Contains("Final") Then
e.Row.Cells(3).BackColor = System.Drawing.Color.Red
e.Row.Cells(3).ForeColor = System.Drawing.Color.White
ElseIf e.Row.Cells(3).Text.Contains("Verbal") Then
e.Row.Cells(3).BackColor = System.Drawing.Color.Yellow
e.Row.Cells(3).ForeColor = System.Drawing.Color.Black
Else
e.Row.Cells(3).BackColor = System.Drawing.Color.Orange
e.Row.Cells(3).ForeColor = System.Drawing.Color.White
End If
End If
End Sub
Thanks for the response, I may have left out some critical details on this. The label that resides in the Template filed has text something like 'Xxxxxxxxx Final xxxxxxxxx or Xxxxxxx Written xxxxxxx' so trim wouldn't make much of a difference. The above
code minus finding the label part worked fine until I had to give the group edit ability to the record and only make one field of the gridview be actionable. I have included the gridview markup as well here to view. It seems like after the first row it
stops looking for the label for some reason and does not even attempt to format past that because the last part of the condition clause just colors the cell orange regardless or content it is just leaving it the default color.
Protected Sub gv_Writeups_RowDataBound(sender As Object, e As GridViewRowEventArgs)
Handles gv_Writeups.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim OccurrenceTitle As String = DataBinder.Eval(e.Row.DataItem,"WriteUpDescriptoin")
Programming to simplify, don't look for difficult way
Suwandi - Non Graduate Programmer
Member
3 Points
6 Posts
Gridview conditional formatting Item Template
Mar 08, 2017 07:29 PM|Menorel|LINK
Before you go, oh no not another one, I have searched and searched for this and some come close but not exactly addressing what is going on.
I have a Gridview and the 4th column of this control is a template field. What I am trying to do is color this cell based on the text that the label has in it. The code below works but I am noticing now that it only works on the first row and not the next row. The code looks for a label if the RowState is normal and a different one if the RowState is edit. What am I missing?
All-Star
52503 Points
15665 Posts
Re: Gridview conditional formatting Item Template
Mar 09, 2017 12:45 AM|oned_gk|LINK
Try use trim and/or use tolower
Suwandi - Non Graduate Programmer
Member
3 Points
6 Posts
Re: Gridview conditional formatting Item Template
Mar 09, 2017 02:57 PM|Menorel|LINK
Thanks for the response, I may have left out some critical details on this. The label that resides in the Template filed has text something like 'Xxxxxxxxx Final xxxxxxxxx or Xxxxxxx Written xxxxxxx' so trim wouldn't make much of a difference. The above code minus finding the label part worked fine until I had to give the group edit ability to the record and only make one field of the gridview be actionable. I have included the gridview markup as well here to view. It seems like after the first row it stops looking for the label for some reason and does not even attempt to format past that because the last part of the condition clause just colors the cell orange regardless or content it is just leaving it the default color.
All-Star
52503 Points
15665 Posts
Re: Gridview conditional formatting Item Template
Mar 09, 2017 05:08 PM|oned_gk|LINK
Try this
Suwandi - Non Graduate Programmer
Member
3 Points
6 Posts
Re: Gridview conditional formatting Item Template
Mar 09, 2017 05:27 PM|Menorel|LINK
Your the man, worked like a charm and allowed me to simplify the code.
Thanks.