locked
Gridview with Row Color based on Value (Yes/No) RRS feed

  • Question

  • User-973788521 posted

    Hello, below is the code for the Gridview. I want to set any row where Existing Note? = YES to RED. 

    I've read a million articles on RowDataBound and it's not working. Thoughts? (code tested is below the ASP code.)

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                               CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="White"
                               GridLines="Vertical" OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="500">
                               <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                               <Columns>
                                  <asp:TemplateField>
                                     <ItemTemplate>
                                        <asp:LinkButton Text="Select" ID="lnkSelect" runat="server" CommandName="Select" />
                                     </ItemTemplate>
                                  </asp:TemplateField>
                                  <asp:BoundField DataField="PatientName" HeaderText="Patient Name" SortExpression="PatientName" />
                                  <asp:BoundField DataField="PatientId" HeaderText="Patient Id" SortExpression="PatientId" />
                                  <asp:BoundField DataField="HasNote" HeaderText="Existing Note?" SortExpression="HasNote" />
                               </Columns>
                               <EditRowStyle BackColor="#999999" />
                               <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                               <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                               <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                               <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                               <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                               <SortedAscendingCellStyle BackColor="#E9E7E2" />
                               <SortedAscendingHeaderStyle BackColor="#506C8C" />
                               <SortedDescendingCellStyle BackColor="#FFFDF8" />
                               <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                            </asp:GridView>
                            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HBMConnectionString %>" SelectCommand="GetPatientData" SelectCommandType="StoredProcedure">
                               <SelectParameters>
                                  <asp:ControlParameter ControlID="txtFirstname" Name="p1" PropertyName="Text" Type="String" />
                               </SelectParameters>
                            </asp:SqlDataSource>
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
          {
             if (e.Row.RowType == DataControlRowType.DataRow)
             {
                var getValue = e.Row.Cells[2].Text;
                string setColorClass = string.Empty;
                if (getValue == "Yes")
                {
                   setColorClass = "dangerFailed";
                }
                else if (getValue == "No")
                {
                   setColorClass = "defaultColor"; // setting red color class
                }
                e.Row.CssClass = setColorClass;
             }
          }
    Thursday, January 12, 2017 11:43 PM

Answers

  • User2103319870 posted

    You can change the background color of gridview row like given below

     protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.Cells[2].Text.ToLower().Trim() == "yes")
                    {
                        e.Row.BackColor = System.Drawing.Color.Red;
                    }
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 13, 2017 12:06 AM

All replies

  • User2103319870 posted

    You can change the background color of gridview row like given below

     protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.Cells[2].Text.ToLower().Trim() == "yes")
                    {
                        e.Row.BackColor = System.Drawing.Color.Red;
                    }
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 13, 2017 12:06 AM
  • User-973788521 posted

    I had to tweak the post because of my mistake, not yours. 

    3, not 2 -- if (e.Row.Cells[3].Text.ToLower().Trim() == "yes")

    Thank you

    Friday, January 13, 2017 12:13 AM