locked
Gridview conditional formatting Item Template RRS feed

  • Question

  • User1546753374 posted

    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

    Wednesday, March 8, 2017 7:29 PM

Answers

  • User-1716253493 posted

    Try this

    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")
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 9, 2017 5:08 PM

All replies

  • User-1716253493 posted

    Try use trim and/or use tolower

    Dim OccurrenceTitle As String = lblOccurrenceTitle.Text.Trim().ToLower()
                Select Case OccurrenceTitle
                    Case "final"
                        e.Row.Cells(3).BackColor = System.Drawing.Color.Red
                        e.Row.Cells(3).ForeColor = System.Drawing.Color.White
                    Case "verbal"
                        e.Row.Cells(3).BackColor = System.Drawing.Color.Yellow
                        e.Row.Cells(3).ForeColor = System.Drawing.Color.Black
                    Case Else
                        e.Row.Cells(3).BackColor = System.Drawing.Color.Orange
                        e.Row.Cells(3).ForeColor = System.Drawing.Color.White
                End Select



    Thursday, March 9, 2017 12:45 AM
  • User1546753374 posted

    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.

    <asp:GridView ID="gv_Writeups" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="WriteUpID" DataSourceID="SQLWriteups" ForeColor="Black" style="font-size: small">
                        <Columns>
                            <asp:CommandField ShowEditButton="True" />
                            <asp:BoundField DataField="WriteUpID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="WriteUpID" />
                            <asp:TemplateField HeaderText="Date">
                                <EditItemTemplate>
                                    <asp:Label ID="lbl_Edit_Date" runat="server" Text='<%# Eval("DateIssued", "{0:d}") %>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Item_Date" runat="server" Text='<%# Eval("DateIssued", "{0:d}") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Description">
                                <EditItemTemplate>
                                    <asp:Label ID="lbl_Edit_Decp" runat="server" Text='<%# Eval("WriteUpDescriptoin") %>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Item_Decp" runat="server" Text='<%# Eval("WriteUpDescriptoin") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Total Points">
                                <EditItemTemplate>
                                    <asp:Label ID="lbl_Edit_TP" runat="server" Text='<%# Eval("TotalPoints") %>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Item_TP" runat="server" Text='<%# Eval("TotalPoints") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="RollsOffIn" HeaderText="Rolls Off" SortExpression="RollsOffIn" ReadOnly="True" />
                            <asp:CheckBoxField DataField="Delivered" HeaderText="Delivered" SortExpression="Delivered" />
                            <asp:BoundField DataField="DeliveredBy" HeaderText="Delivered By" SortExpression="DeliveredBy" Visible="False" />
                            <asp:BoundField DataField="DeliveredOn" HeaderText="Delivered On" SortExpression="DeliveredOn" Visible="False" />
                        </Columns>
                        <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
                        <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
                        <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
                        <SortedAscendingCellStyle BackColor="#F7F7F7" />
                        <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
                        <SortedDescendingCellStyle BackColor="#E5E5E5" />
                        <SortedDescendingHeaderStyle BackColor="#242121" />
                    </asp:GridView>

    Thursday, March 9, 2017 2:57 PM
  • User-1716253493 posted

    Try this

    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")
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 9, 2017 5:08 PM
  • User1546753374 posted

    Your the man, worked like a charm and allowed me to simplify the code.

    Thanks.

    Thursday, March 9, 2017 5:27 PM