Answered by:
how to delete selected rows in repeater

Question
-
User1717218719 posted
What I am trying to do:
I am looking to delete the selected row in a repeater. the user selects the row using a checkbox. if at least one or more rows have checkbox checked and the link button "delete selected" is clicked then those rows are removed and the data table rebinds.What I have tried:
I have the following code which is not working. I am not getting any errors but nothing is happening. Any help with why this could be happening would be great.Note: argcmndsrc = linkbutton, intIdx = integer, repeater1 = repeater name, GetItms = function that builds datatable
'CheckBox If argcmndsrc.CommandName = "DeleteSelected" Then intIdx = argcmndsrc.CommandArgument itmRpt = Me.repeater1.Items(intIdx) Dim dtbl As DataTable = GetItms() Dim cbox As CheckBox = TryCast(e.Item.FindControl("CheckBox1"), CheckBox) If cbox IsNot Nothing Then dtbl.Rows.RemoveAt(intIdx) '-- rebind after delete Me.repeater1.DataSource = dtbl Me.repeater1.DataBind() End If End If
Tuesday, June 25, 2019 9:17 AM
Answers
-
User288213138 posted
Hi E.RU,
According to your description, I made a demo for your reference.
In the DeleteRow event, I take the index of the current row and delete the current row according to the index.
In the ClearAll event, I use the clear() method to delete the data inside the DataTable.
In the DeleteSelected event, I delete the data for the currently selected row based on the CustomerId.
The code:
Aspx:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#tblCustomers [id*=CheckBox2]").click(function () {
if ($(this).is(":checked")) {
$("#tblCustomers [id*=CheckBox1]").attr("checked", "checked");
} else {
$("#tblCustomers [id*=CheckBox1]").removeAttr("checked");
}
});
});
</script> <div> <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand"> <HeaderTemplate> <table border="1" id="tblCustomers"> <tr> <th scope="col" style="width: 80px">CustomerId </th> <th scope="col" style="width: 120px">Name </th> <th scope="col" style="width: 100px">Country </th> <th> <asp:CheckBox ID="CheckBox2" runat="server" /></th> <th></th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("CustomerId") %>' /> </td> <td> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("Name") %>' /> </td> <td> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("Country") %>' /> </td> <td> <asp:CheckBox ID="CheckBox1" runat="server" /> </td> <td> <asp:LinkButton ID="lnkUpdate" Text="Delete" runat="server" CommandName="DeleteRow" CommandArgument='<%# Container.ItemIndex %>'/> </td> </tr> </ItemTemplate> <FooterTemplate> </table> <asp:LinkButton ID="LinkButton3" Text="ClearAll" runat="server" CommandName="ClearAll" OnClick="LinkButton3_Click"/><br /> <asp:LinkButton ID="LinkButton2" Text="Delete Selected" runat="server" CommandName="DeleteSelected" CommandArgument='<%# Container.ItemIndex %>' OnClick="LinkButton2_Click"/> </FooterTemplate> </asp:Repeater> </div> Aspx.cs: Class SurroundingClass Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not IsPostBack Then Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn(2) {New DataColumn("CustomerId"), New DataColumn("Name"), New DataColumn("Country")}) dt.Rows.Add(1, "name1", "country1") dt.Rows.Add(2, "name2", "country2") dt.Rows.Add(3, "name3", "country3") dt.Rows.Add(4, "name4", "country4") ViewState("dt") = dt Bind() End If End Sub Public Sub Bind() Repeater1.DataSource = TryCast(ViewState("dt"), DataTable) Repeater1.DataBind() End Sub Protected Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs) For item As Integer = 0 To Me.Repeater1.Items.Count - 1 Dim chb As CheckBox = TryCast(Me.Repeater1.Items(item).FindControl("CheckBox1"), CheckBox) If chb.Checked Then If e.CommandName = "DeleteRow" Then Dim index As Integer = Convert.ToInt32(e.CommandArgument) Dim dt As DataTable = TryCast(ViewState("dt"), DataTable) dt.Rows(index).Delete() ViewState("dt") = dt Bind() End If End If Next End Sub Protected Sub LinkButton3_Click(ByVal sender As Object, ByVal e As EventArgs) Dim chb As CheckBox = TryCast(Repeater1.Controls(0).Controls(0).FindControl("CheckBox2"), CheckBox) If chb.Checked Then Dim dt As DataTable = TryCast(ViewState("dt"), DataTable) dt.Clear() ViewState("dt") = dt Bind() End If End SubThe result:
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 26, 2019 10:10 AM
All replies
-
User-1038772411 posted
Hello E.RU,
Try with below example, I hope this will help you
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not IsPostBack Then BindGridview() End If End Sub Private Function GetData(ByVal query As String) As DataSet Dim conString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString Dim cmd SqlCommand = New (query) Dim con SqlConnection = New (conString) Dim sda SqlDataAdapter = New SqlDataAdapter() cmd.Connection = con sda.SelectCommand = cmd Dim ds DataSet = New DataSet() sda.Fill(ds) Return ds End Function Public Sub BindGridview() GridEmployee.DataSource = GetData("SELECT ID,EmployeeName, EmployeeAge, EmployeeDesc, EmployeeExperience FROM Employee") GridEmployee.DataBind() End Sub Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) For Each gvrow As GridViewRow In GridEmployee.Rows 'Finiding checkbox control in gridview for particular row Dim chkdelete As CheckBox = CType(gvrow.FindControl("chkSelect"),CheckBox) 'Condition to check checkbox selected or not If chkdelete.Checked Then 'Getting EmployeeID of particular row using datakey value Dim EmployeeID As Integer = Convert.ToInt32(GridEmployee.DataKeys(gvrow.RowIndex).Value) 'Getting Connection String from Web.Config Dim conString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString Dim con SqlConnection = New (conString) con.Open() Dim cmd SqlCommand = New (("DELETE FROM Employee where ID=" + EmployeeID), con) cmd.ExecuteNonQuery() con.Close() End If Next BindGridview() End Sub
For more details kindly refer the following link
Reference : http://www.thedevelopertips.com/DotNet/ASPDotNet/Delete-Gridview-Selected-Rows.aspx?Id=75
Thank you
Tuesday, June 25, 2019 9:43 AM -
User1717218719 posted
Hi
Thankyou for your reply!!
However, as I am using a repeater and a datatable not a gridview or sql database. how would I go about coding for this situation?
Many Thanks
Tuesday, June 25, 2019 10:23 AM -
User-1038772411 posted
Hello E.RU,
Try with this, I hope this will work for you
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim dt As DataTable = New DataTable() dt.Columns.Add("ID") dt.Rows.Add(1) dt.Rows.Add(2) dt.Rows.Add(3) rptCustomers.DataSource = dt rptCustomers.DataBind() End If End Sub Protected Sub getCheckedRow(ByVal sender As Object, ByVal e As EventArgs)
Dim value As String = "" For Each item As RepeaterItem In rptCustomers.Items Dim chk As CheckBox = TryCast(item.FindControl("rbt_etails"), CheckBox) If chk.Checked Then
rptCustomers.item.Delete() End If Next rptCustomers.DataBind() End SubReferences
https://www.dreamincode.net/forums/topic/173693-delete-row-from-datatable/
Thank you
Tuesday, June 25, 2019 10:51 AM -
User1717218719 posted
Thank you I have tried this code and get the error "'item' is not a member of 'Repeater'. on the line rptCustomers.item.Delete() .
Also From debugging I have just noticed that when I put a message box in my code it does not result to anything. so I checked my link button code and my checkbox code but cant seem to find any issues. I have no idea why this is.
here is my asp code:
<asp:Panel ID="pnlLbl" runat="server"> <tr> <td> <asp:LinkButton ID="btnEdtItm" Text="Edit" runat="server" CommandName="Edit" CommandArgument='<%# Container.ItemIndex %>' /> <asp:LinkButton ID="btnDelItm" Text="Delete" runat="server" CommandName="DeleteRow" CommandArgument='<%# Container.ItemIndex %>' OnClientClick="return confirm('Do you want to delete this row?');" /> </td> <td></td> <td> <asp:CheckBox ID="CheckBox1" runat="server"/></td> <%--<asp:CheckBox ID="CheckBox2" runat="server" CommandName="CheckBox" CommandArgument='<%# Container.ItemIndex %>' /></td>--%> </tr> </asp:Panel> </ItemTemplate> <FooterTemplate> <tr> <td></td> <td> <asp:Label CssClass="Label" ID="TtlLbl" runat="server" Text="Total"></asp:Label></td> <td> <asp:Label CssClass="Label" ID="Label2" runat="server"></asp:Label></td> <td> <asp:Label CssClass="Label" ID="Label3" runat="server"></asp:Label></td> <td> <asp:Label CssClass="Label" ID="Label4" runat="server"></asp:Label></td> <td> <td></td> </tr> </table> <asp:LinkButton ID="LinkButton3" Text="ClearAll" runat="server" CommandName="ClearAll" OnClientClick="return confirm('Do you want to delete All?');" /> <asp:LinkButton ID="LinkButton2" Text="Delete Selected" runat="server" CommandName="DeleteSelected" CommandArgument='<%# Container.ItemIndex %>' OnClientClick="return confirm('Do you want to delete Selected row?');"/> </FooterTemplate>
Tuesday, June 25, 2019 11:04 AM -
User-1038772411 posted
Hello E.RU
Use item.Remove() inplace of rptCustomers.item.Delete()
item.Remove()
Try with this.
Thank you
Tuesday, June 25, 2019 11:44 AM -
User1717218719 posted
I still recieve the same error unfortunately?
Also I still have an issue with even getting a msgbox to work. thank you for your help so far !!! :)
Tuesday, June 25, 2019 12:01 PM -
User288213138 posted
Hi E.RU,
According to your description, I made a demo for your reference.
In the DeleteRow event, I take the index of the current row and delete the current row according to the index.
In the ClearAll event, I use the clear() method to delete the data inside the DataTable.
In the DeleteSelected event, I delete the data for the currently selected row based on the CustomerId.
The code:
Aspx:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#tblCustomers [id*=CheckBox2]").click(function () {
if ($(this).is(":checked")) {
$("#tblCustomers [id*=CheckBox1]").attr("checked", "checked");
} else {
$("#tblCustomers [id*=CheckBox1]").removeAttr("checked");
}
});
});
</script> <div> <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand"> <HeaderTemplate> <table border="1" id="tblCustomers"> <tr> <th scope="col" style="width: 80px">CustomerId </th> <th scope="col" style="width: 120px">Name </th> <th scope="col" style="width: 100px">Country </th> <th> <asp:CheckBox ID="CheckBox2" runat="server" /></th> <th></th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("CustomerId") %>' /> </td> <td> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("Name") %>' /> </td> <td> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("Country") %>' /> </td> <td> <asp:CheckBox ID="CheckBox1" runat="server" /> </td> <td> <asp:LinkButton ID="lnkUpdate" Text="Delete" runat="server" CommandName="DeleteRow" CommandArgument='<%# Container.ItemIndex %>'/> </td> </tr> </ItemTemplate> <FooterTemplate> </table> <asp:LinkButton ID="LinkButton3" Text="ClearAll" runat="server" CommandName="ClearAll" OnClick="LinkButton3_Click"/><br /> <asp:LinkButton ID="LinkButton2" Text="Delete Selected" runat="server" CommandName="DeleteSelected" CommandArgument='<%# Container.ItemIndex %>' OnClick="LinkButton2_Click"/> </FooterTemplate> </asp:Repeater> </div> Aspx.cs: Class SurroundingClass Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not IsPostBack Then Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn(2) {New DataColumn("CustomerId"), New DataColumn("Name"), New DataColumn("Country")}) dt.Rows.Add(1, "name1", "country1") dt.Rows.Add(2, "name2", "country2") dt.Rows.Add(3, "name3", "country3") dt.Rows.Add(4, "name4", "country4") ViewState("dt") = dt Bind() End If End Sub Public Sub Bind() Repeater1.DataSource = TryCast(ViewState("dt"), DataTable) Repeater1.DataBind() End Sub Protected Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs) For item As Integer = 0 To Me.Repeater1.Items.Count - 1 Dim chb As CheckBox = TryCast(Me.Repeater1.Items(item).FindControl("CheckBox1"), CheckBox) If chb.Checked Then If e.CommandName = "DeleteRow" Then Dim index As Integer = Convert.ToInt32(e.CommandArgument) Dim dt As DataTable = TryCast(ViewState("dt"), DataTable) dt.Rows(index).Delete() ViewState("dt") = dt Bind() End If End If Next End Sub Protected Sub LinkButton3_Click(ByVal sender As Object, ByVal e As EventArgs) Dim chb As CheckBox = TryCast(Repeater1.Controls(0).Controls(0).FindControl("CheckBox2"), CheckBox) If chb.Checked Then Dim dt As DataTable = TryCast(ViewState("dt"), DataTable) dt.Clear() ViewState("dt") = dt Bind() End If End SubThe result:
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 26, 2019 10:10 AM -
User1717218719 posted
I got it thanks a million
Wednesday, June 26, 2019 10:42 AM