hi....
i have been writing number of post about grid view.Now Here i am going to explain How to delete Multiple Records using check Box in grid view.
For your refrence In previous post i have written about How to update and Edit value in Grid View.
http://vbdotnetaddict.blogspot.com/2011/02/groid-view-load-and-update-sql-data.html
Steps To Follow.
First in client side you have to add grid view,add check box in Header tamplate and item template,Refer the below client Side coding
Second in Server side you have to add declare function checkbox selected index change ,this function used to select all the checkbox in the grid view.Refer the below server side coding ...
Client Side Coding :
<asp:GridView ID="GridView1" runat="server"
CellPadding="3" GridLines="Vertical"
AutoGenerateColumns="False" Height="155px" Width="582px" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
AllowPaging="True" DataKeyNames="Number" >
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="left" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns >
<asp:TemplateField HeaderText ="s.no">
<ItemTemplate >
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText ="Phone Number" DataField ="Number" />
<asp:TemplateField HeaderText ="NAME" >
<ItemTemplate >
<%#Eval("Name")%>
</ItemTemplate>
<EditItemTemplate >
<cc1:MacroWebTextBox ID="txtrwname" runat="server" Text ='<%#Bind("Name")%>' >
</cc1:MacroWebTextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="ADDRESS">
<ItemTemplate >
<%#Eval("Address")%>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
<EditItemTemplate >
<cc1:MacroWebTextBox ID="txtrwaddress" runat="server" Text='<%#bind("Address")%> ' Validate ="IsAlphaNum" ></cc1:MacroWebTextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderTEXT="CITY" >
<ItemTemplate >
<%#Eval("City")%>
</ItemTemplate>
<EditItemTemplate>
<cc1:MacroWebTextBox ID="txtrwcity" runat="server" Text='<%#bind("City")%>'></cc1:MacroWebTextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="STATE">
<ItemTemplate>
<%#Eval("State")%>
</ItemTemplate>
<EditItemTemplate>
<cc1:MacroWebTextBox ID="txtrwstate" runat="server" Text='<%#bind("State")%> ' Validate ="IsAlphaNum ">
</cc1:MacroWebTextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText ="modify" ShowDeleteButton ="false" ShowEditButton ="true" EditText ="modify" />
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" CommandName="Delete" runat="server" OnClientClick ="return confirm('are you sure')">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText ="Email" DataField ="email" />
<asp:TemplateField headertext="checkbox">
<ItemTemplate >
<asp:CheckBox ID="gridchkbox" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="CheckBoxall" runat="server" AutoPostBack ="true" OnCheckedChanged ="checkbox1_checkedchanged"/>
</HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Server Side Coding:
This code is used to delete the selected record in the grid view
Protected Sub btndelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btndelete.Click
Dim ch As CheckBox
Dim number As String
For i As Integer = 0 To GridView1.Rows.Count - 1
ch = CType(GridView1.Rows(i).Cells(9).FindControl("gridchkbox"), CheckBox)
If ch.Checked = True Then
number = GridView1.Rows(i).Cells(1).Text
sel = "Delete from phone where Number='" & number & "'"
'Excute the query for delete the records
execute command(sel)
'
'
End If
Next
loadde()
End Sub
This code is used to select and Deselect CheckBox
protected sub checkbox1_CheckedChanged (ByVal sender As Object, ByVal e As EventArgs)
Dim chkbxall As CheckBox
chkbxall = CType(GridView1.HeaderRow.FindControl("checkboxall"), CheckBox)
If chkbxall.Checked = True Then
For Each row As GridViewRow In GridView1.Rows
Dim chkall As CheckBox
chkall = CType(row.FindControl("gridchkbox"), CheckBox)
If chkall.Checked = False Then
chkall.Checked = True
End If
Next
Else
For Each row As GridViewRow In GridView1.Rows
Dim chkall As CheckBox
chkall = CType(row.FindControl("gridchkbox"), CheckBox)
If chkall.Checked = True Then
chkall.Checked = False
End If
Next
End If
End Sub
i hope this one helpful to you..
Plz post your valuable comments................
0 comments:
Post a Comment