jeudi 29 juin 2017

change color of databound gridview checkboxes depending on their bool value - either checked or unchecked (true/false)

I have a gridview in asp.net web form. It is bound to a database and I have a column where the data type is bit. This has automatically changed the bound field to checkboxes. I like the checkboxes but would like to conditionally format the cell they are in based on whether they are checked or not.

 protected void allgv_RowDataBound(object sender, GridViewRowEventArgs e)
    {


        if (((CheckBox)e.Row.Cells[4].Controls[0]).Checked == true)


        {
            e.Row.Cells[4].ControlStyle.BackColor = System.Drawing.Color.Red;
        }


    }

The above code throws an exception of "Object reference not set to an instance of an object."

I feel like I'm on the right lines but just need a little help.

Here is the HTML if it helps:

   <asp:GridView ID="allgv" runat="server" AutoGenerateColumns="False" CellPadding="5" CellSpacing="1" DataSourceID="SqlDataSource5" Font-Names="Raleway" ForeColor="White" Width="100%" OnRowDataBound="allgv_RowDataBound">
    <Columns>
        <asp:BoundField DataField="Competitor" HeaderText="Name" SortExpression="Competitor" />
        <asp:BoundField DataField="Detail" HeaderText="Detail" SortExpression="Detail" />
        <asp:BoundField DataField="Decision" HeaderText="Decision" SortExpression="Decision" />
        <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
        <asp:checkboxfield DataField="checkboxfield1" HeaderText="checkboxfield1" SortExpression="checkboxfield1"  />
            <asp:checkboxfield DataField="checkboxfield2" HeaderText="checkboxfield2" SortExpression="checkboxfield2" />
            <asp:checkboxfield DataField="checkboxfield3" HeaderText="checkboxfield3" SortExpression="checkboxfield3" />
            <asp:checkboxfield DataField="checkboxfield4" HeaderText="checkboxfield4" SortExpression="checkboxfield4" />
            <asp:checkboxfield DataField="Other" HeaderText="Other" SortExpression="Other" />
    </Columns>
</asp:GridView>




Aucun commentaire:

Enregistrer un commentaire