Click here to Skip to main content
15,891,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to change a the colors and font of a row in a gridview by the state of a checkbox.

I have a databound gridview that has a cancelled checkbox. I would like to change the text to red and font to strikethrough when the box is checked. I would like to do this without Java if possible.

<pre lang="xml"><asp:GridView ID="GridViewATC" runat="server" AllowSorting="True"
                AutoGenerateColumns="False" CssClass="gridlineClass"
                HeaderStyle-CssClass="freezing" GridLines="Both" BorderColor="Red"
                DataKeyNames="ID" DataSourceID="AccessDataSource1"
                EmptyDataText="There are no data records to display."
                ForeColor="#333333"
                style="border-style: solid; border-color: #FFFF00; margin-top: 0px;"
                PageSize="15" RowHeaderColumn="cancelled" CellPadding="4"
                onselectedindexchanged="GridViewATC_SelectedIndexChanged" Enabled="false" >
            <AlternatingRowStyle BackColor="White" BorderStyle="Solid" BorderWidth="9px" BorderColor="Red" />
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
                    ButtonType="Button" InsertVisible="False" />
                <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
                    SortExpression="ID" ApplyFormatInEditMode="True" />
                <asp:BoundField DataField="Short_Name" HeaderText="Short_Name"
                    SortExpression="Short_Name" ApplyFormatInEditMode="True" >
                <ControlStyle Width="100px" />
                <HeaderStyle Width="100px" />
                <ItemStyle Width="100px" />
                </asp:BoundField>
                <asp:BoundField DataField="Users_DN" dataformatstring="{0:(###) ###-####}" HTMLEncode="false" HeaderText="Users_DN"
                    SortExpression="Users_DN" ApplyFormatInEditMode="True" >
                <ControlStyle Width="100px" />
                <FooterStyle Width="200px" Wrap="False" />
                <HeaderStyle HorizontalAlign="Center" Width="100px" Wrap="False" />
                <ItemStyle Width="100px" Wrap="False" />
                </asp:BoundField>
                <asp:BoundField DataField="Instant_Meeting_Owner"
                    HeaderText="Instant Meeting Owner" SortExpression="Instant_Meeting_Owner"
                    ApplyFormatInEditMode="True" >
                <ControlStyle Width="100px" />
                <HeaderStyle Width="100px" Wrap="true" />
                <ItemStyle Width="120px" Wrap="true" />
                </asp:BoundField>
                <asp:BoundField DataField="column1" dataformatstring="{0:(###) ###-####}" HTMLEncode="false" HeaderText="Dial-In Number"
                    SortExpression="column1" ApplyFormatInEditMode="True" >
                <ControlStyle Width="100px" />
                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="155px" />
                <ItemStyle Wrap="True" Width="100px" />
                </asp:BoundField>
                <asp:BoundField DataField="Participant_Code" HeaderText="Participant Code"
                    SortExpression="Participant_Code" ApplyFormatInEditMode="True" >
                <ControlStyle Width="100px" />
                <FooterStyle Width="100px" />
                <HeaderStyle Width="100px" />
                <ItemStyle Wrap="True" Width="100px" />
                </asp:BoundField>
                <asp:BoundField DataField="Leader_Code" HeaderText="Leader Code"
                    SortExpression="Leader_Code" ApplyFormatInEditMode="True" >
                <ControlStyle Width="80px" />
                <FooterStyle Width="100px" />
                <HeaderStyle Width="100px" />
                <ItemStyle Width="80px" />
                </asp:BoundField>
                <asp:BoundField DataField="Conf_ID" HeaderText="Conf_ID"
                    SortExpression="Conf_ID" ApplyFormatInEditMode="True" />
                <asp:BoundField DataField="column2" HeaderText="# of Lines"
                    SortExpression="column2" ApplyFormatInEditMode="True" >
                <ControlStyle Width="20px" />
                <HeaderStyle Width="50px" />
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" />
                </asp:BoundField>
                <asp:BoundField DataField="Date_verified" DataFormatString="{0:M/dd/yyyy}" HeaderText="Date_verified"
                    SortExpression="Date_verified" ApplyFormatInEditMode="True" >
                <ControlStyle Width="100px" />
                <FooterStyle Width="100px" />
                <HeaderStyle Width="100px" />
                <ItemStyle Width="100px" />
                </asp:BoundField>
                <asp:CheckBoxField DataField="cancelled" HeaderText="cancelled"
                    SortExpression="cancelled" >
                <ControlStyle Font-Strikeout="True" />
                </asp:CheckBoxField>
                <asp:CheckBoxField DataField="Verified" HeaderText="Verified"
                    SortExpression="Verified" />
                <asp:BoundField DataField="Date_Cancelled" DataFormatString="{0:M/dd/yyyy}" HeaderText="Date_Cancelled"
                    SortExpression="Date_Cancelled" ApplyFormatInEditMode="True" >
                <ControlStyle BorderStyle="Solid" BorderWidth="2px" Width="100px" />
                <HeaderStyle Width="100px" />
                <ItemStyle Width="100px" />
                </asp:BoundField>
                <asp:TemplateField Visible="False">
                    <ItemTemplate>
                        <font size="2">&lt;<font color="#a52a2a">asp:CheckBox</font>
                        <font color="#0000ff"><font color="#ff0000">id</font>=&quot;CheckBox1&quot;
                        <font color="#ff0000">runat</font>=&quot;server&quot; <font color="#ff0000">AutoPostBack</font>=&quot;True&quot;
                        <font color="#ff0000">OnCheckedChanged</font>=&quot;Check_Clicked&quot;</font><font
                            color="#red">&gt;</font>&lt;/&lt; <font <="" color="#a52a2a">asp:CheckBox</font>&gt;<br />
                        </font>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" BorderColor="Red" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" BorderColor="Red"/>
                <AlternatingRowStyle BackColor="LightBlue" bordercolor="Red" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" BorderColor="Red" />
                 <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />
            </asp:GridView>



Here is my backend
public partial class _Default : System.Web.UI.Page<br />
    {<br />
        protected void atcadd_Click(object sender, System.EventArgs e)<br />
    {<br />
        Response.Redirect("atcadd.aspx");<br />
    }<br />
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)<br />
        if (e.Row.RowType == DataControlRowType.DataRow)<br />
        System.Data.DataRowView cancelled = (System.Data.DataRowView)e.Row.DataItem;<br />
        if (checked == (true))<br />
        if (CheckBox.Equals = (checked(true)))<br />
            e.Row.BackColor = System.Drawing.Color.Red;<br />
            e.Row.ForeColor = System.Drawing.Color.White;<br />
            e.Row.Font = System.Drawing.Font.Strikeout;<br />
        public System.Data.DataColumn cancelled { get; set; }<br />
    }<br />
}
Posted

1 solution

If you want to set color/style of row/rows from Code behind file then please remove Color/style set in HTML view. Color/Style which are mentioned HTML is basically overwrites color/style set from code behind.

Please let me know if it helps you,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900