This example explains how to use Check All Checkbox In GridView To Bulk Edit
Or Update in ASP.NET with C# and VB.NET. I have put a checkBox in header
Template of gridview which on checking will check all rows in gridview using
server side code to implement CheckAll CheckBox functionality.
Html SOURCE OF GRIDVIEW
C# CODE
VB.NET
Hope this helps.
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
protected void chkSelectAll_CheckedChanged
(object sender, EventArgs e)
{
CheckBox chkAll =
(CheckBox)GridView1.HeaderRow.FindControl("chkSelectAll");
if (chkAll.Checked == true)
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
CheckBox chkSel =
(CheckBox)gvRow.FindControl("chkSelect");
chkSel.Checked = true;
TextBox txtname = (TextBox)gvRow.FindControl("txtName");
TextBox txtlocation = (TextBox)gvRow.FindControl("txtLocation");
txtname.ReadOnly = false;
txtlocation.ReadOnly = false;
txtname.ForeColor = System.Drawing.Color.Black;
txtlocation.ForeColor = System.Drawing.Color.Black;
}
}
else
{
foreach (GridViewRow gvRow in GridView1.Rows)
{
CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
chkSel.Checked = false;
TextBox txtname = (TextBox)gvRow.FindControl("txtName");
TextBox txtlocation = (TextBox)gvRow.FindControl("txtLocation");
txtname.ReadOnly = true;
txtlocation.ReadOnly = true;
txtname.ForeColor = System.Drawing.Color.Blue;
txtlocation.ForeColor = System.Drawing.Color.Blue;
}
}
}
VB.NET
Protected Sub chkSelectAll_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim chkAll As CheckBox = DirectCast(GridView1.HeaderRow.FindControl("chkSelectAll"), CheckBox)
If chkAll.Checked = True Then
For Each gvRow As GridViewRow In GridView1.Rows
Dim chkSel As CheckBox = DirectCast(gvRow.FindControl("chkSelect"), CheckBox)
chkSel.Checked = True
Dim txtname As TextBox = DirectCast(gvRow.FindControl("txtName"), TextBox)
Dim txtlocation As TextBox = DirectCast(gvRow.FindControl("txtLocation"), TextBox)
txtname.[ReadOnly] = False
txtlocation.[ReadOnly] = False
txtname.ForeColor = System.Drawing.Color.Black
txtlocation.ForeColor = System.Drawing.Color.Black
Next
Else
For Each gvRow As GridViewRow In GridView1.Rows
Dim chkSel As CheckBox = DirectCast(gvRow.FindControl("chkSelect"), CheckBox)
chkSel.Checked = False
Dim txtname As TextBox = DirectCast(gvRow.FindControl("txtName"), TextBox)
Dim txtlocation As TextBox = DirectCast(gvRow.FindControl("txtLocation"), TextBox)
txtname.[ReadOnly] = True
txtlocation.[ReadOnly] = True
txtname.ForeColor = System.Drawing.Color.Blue
txtlocation.ForeColor = System.Drawing.Color.Blue
Next
End If
End Sub
Hope this helps.

hey vikash..I am getting error for "GridView1_RowDataBound" and update_click events
ReplyDeletewould you please upload all the codes for bulk update or delete in gridview