Last post Jan 13, 2017 12:13 AM by doctork32
Member
16 Points
44 Posts
Jan 12, 2017 11:43 PM|doctork32|LINK
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; } }
All-Star
50831 Points
9895 Posts
Jan 13, 2017 12:06 AM|A2H|LINK
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; } } }
Jan 13, 2017 12:13 AM|doctork32|LINK
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
Member
16 Points
44 Posts
Gridview with Row Color based on Value (Yes/No)
Jan 12, 2017 11:43 PM|doctork32|LINK
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.)
All-Star
50831 Points
9895 Posts
Re: Gridview with Row Color based on Value (Yes/No)
Jan 13, 2017 12:06 AM|A2H|LINK
You can change the background color of gridview row like given below
Aje
My Blog | Dotnet Funda
Member
16 Points
44 Posts
Re: Gridview with Row Color based on Value (Yes/No)
Jan 13, 2017 12:13 AM|doctork32|LINK
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