Pages - Menu

Wednesday, February 15, 2012

How to Check All Checkbox In GridView To Bulk Edit Or Update ASP.NET

CheckAll CheckBox In GridView to Edit and Update

Check All Checkbox In GridView To Bulk Edit Or Update in ASP.NET with C# and VB.NET.
In this example i am going to describe how to implement CheckAll CheckBox in GridView to check all the rows in gridView.

For this i have put a checkBox in header Template of gridview which on checking will check all the rows in gridview using server side code.



Html Source of gridView
<asp:GridView ID="GridView1" runat="server" 
              DataSourceID="SqlDataSource1" 
              AutoGenerateColumns="false" 
              CellPadding="2" ForeColor="#333333" 
              GridLines="Both" 
              DataKeyNames="ID" 
              OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="CheckAll">
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" runat="server" 
              AutoPostBack="true" 
              OnCheckedChanged="chkSelectAll_CheckedChanged"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" 
              AutoPostBack="true" 
              OnCheckedChanged="chkSelect_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="ID" HeaderText="ID" 
                SortExpression="ID"/>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" 
             Text='<%# Bind("Name") %>' ForeColor="Blue" 
             BorderStyle="none" BorderWidth="0px" 
             ReadOnly="true" >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Location" SortExpression
="Location">
<ItemTemplate>
<asp:TextBox ID="txtLocation" runat="server" 
             Text='<%# Bind("Location") %>' 
             ForeColor="Blue" BorderStyle="none" 
             ReadOnly="true">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Location] FROM [Details]" 
DeleteCommand="DELETE FROM Details WHERE (ID = @ID)" 
UpdateCommand="UPDATE [Details] SET [Name] = @Name, 
               [Location] = @Location WHERE [ID] = @ID">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>

<UpdateParameters>
<asp:Parameter Name="Name" />
<asp:Parameter Name="Location" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>

<asp:Button ID="btnUpdate" runat="server" 
            OnClick="btnUpdate_Click" Text="Update" />
<asp:Button ID="btnDelete" runat="server" 
            OnClick="btnDelete_Click" 
            Text="Delete" />

C# Code behind
01protected void chkSelectAll_CheckedChanged
02                               (object sender, EventArgs e)
03{
04 CheckBox chkAll =
05    (CheckBox)GridView1.HeaderRow.FindControl("chkSelectAll");
06 if (chkAll.Checked == true)
07 {
08   foreach (GridViewRow gvRow in GridView1.Rows)
09   {
10    CheckBox chkSel =
11         (CheckBox)gvRow.FindControl("chkSelect");
12    chkSel.Checked = true;
13    TextBox txtname = (TextBox)gvRow.FindControl("txtName");
14    TextBox txtlocation = (TextBox)gvRow.FindControl("txtLocation");
15    txtname.ReadOnly = false;
16    txtlocation.ReadOnly = false;
17    txtname.ForeColor = System.Drawing.Color.Black;
18    txtlocation.ForeColor = System.Drawing.Color.Black;
19   }
20 }
21 else
22 {
23  foreach (GridViewRow gvRow in GridView1.Rows)
24  {
25   CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
26   chkSel.Checked = false;
27   TextBox txtname = (TextBox)gvRow.FindControl("txtName");
28   TextBox txtlocation = (TextBox)gvRow.FindControl("txtLocation");
29   txtname.ReadOnly = true;
30   txtlocation.ReadOnly = true;
31   txtname.ForeColor = System.Drawing.Color.Blue;
32   txtlocation.ForeColor = System.Drawing.Color.Blue;
33   }
34  }
35}

VB.NET code behind
01Protected Sub chkSelectAll_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
02    Dim chkAll As CheckBox = DirectCast(GridView1.HeaderRow.FindControl("chkSelectAll"), CheckBox)
03    If chkAll.Checked = True Then
04        For Each gvRow As GridViewRow In GridView1.Rows
05            Dim chkSel As CheckBox = DirectCast(gvRow.FindControl("chkSelect"), CheckBox)
06            chkSel.Checked = True
07            Dim txtname As TextBox = DirectCast(gvRow.FindControl("txtName"), TextBox)
08            Dim txtlocation As TextBox = DirectCast(gvRow.FindControl("txtLocation"), TextBox)
09            txtname.[ReadOnly] = False
10            txtlocation.[ReadOnly] = False
11            txtname.ForeColor = System.Drawing.Color.Black
12            txtlocation.ForeColor = System.Drawing.Color.Black
13        Next
14    Else
15        For Each gvRow As GridViewRow In GridView1.Rows
16            Dim chkSel As CheckBox = DirectCast(gvRow.FindControl("chkSelect"), CheckBox)
17            chkSel.Checked = False
18            Dim txtname As TextBox = DirectCast(gvRow.FindControl("txtName"), TextBox)
19            Dim txtlocation As TextBox = DirectCast(gvRow.FindControl("txtLocation"), TextBox)
20            txtname.[ReadOnly] = True
21            txtlocation.[ReadOnly] = True
22            txtname.ForeColor = System.Drawing.Color.Blue
23            txtlocation.ForeColor = System.Drawing.Color.Blue
24        Next
25    End If
26End Sub


Hope this helps

No comments:

Post a Comment