GridView with CheckBox – Select All and Highlight Selected Row in Asp.Net


 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
            DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
                    SortExpression="id" />
                <asp:BoundField DataField="id2" HeaderText="id2" SortExpression="id2" />
                <asp:TemplateField HeaderText="yesNo" SortExpression="yesNo">
                    <EditItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("yesNo") %>' AutoPostBack="true" OnCheckedChanged="CheckBox1_OnCheckedChanged" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("yesNo") %>' AutoPostBack="true" OnCheckedChanged="CheckBox1_OnCheckedChanged" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MSDN_forumConnectionString %>"
            DeleteCommand="DELETE FROM [foo] WHERE [id] = @id" InsertCommand="INSERT INTO [foo] ([id2], [yesNo]) VALUES (@id2, @yesNo)"
            SelectCommand="SELECT [id], [id2], [yesNo] FROM [foo]" UpdateCommand="UPDATE [foo] SET [id2] = @id2, [yesNo] = @yesNo WHERE [id] = @id">
            <DeleteParameters>
                <asp:Parameter Name="id" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="id2" Type="String" />
                <asp:Parameter Name="yesNo" Type="Boolean" />
                <asp:Parameter Name="id" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="id2" Type="String" />
                <asp:Parameter Name="yesNo" Type="Boolean" />
            </InsertParameters>
        </asp:SqlDataSource>
protected void CheckBox1_OnCheckedChanged(object sender, EventArgs e)
    {
         CheckBox ck1 = (CheckBox)sender;

       GridViewRow gdrow = (GridViewRow)ck1.NamingContainer;

           if (((CheckBox)gdrow.FindControl("CheckBox1")).Checked)
        {
           gdrow.BackColor = System.Drawing.Color.Yellow;
        }
        else
        {
          gdrow.BackColor = System.Drawing.Color.Aqua; 
        }
}
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

          if (((CheckBox)e.Row.FindControl("CheckBox1")).Checked)
            {
               e.Row.BackColor = System.Drawing.Color.Yellow;
            }

            else
            {
              e.Row.BackColor = System.Drawing.Color.Aqua;
            }
        }

    }

0 comments:

Post a Comment