I’m trying to delete the selected row in a gridview from the datatable but it seems like it’s not deleting. in if statment Table.Rows.Count = 0 is not working and I’m trying this with one row in the grid view.
Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim Table As New DataTable
Table = GridView1.DataSource
Dim iOriPrice As Double
iOriPrice = Table.Rows(index).Item("sPrice")
Table.Rows(index).Delete()
'reset table and set to gridview
If Table.Rows.Count = 0 Then
Table.Reset()
GridView1.DataSource = Table
GridView1.DataBind()
lblTotalAm.Text = ""
lblTotalMsg.Text = "Shopping Cart is empty"
Session.Add("BuyTable", Table)
btnBuyNow.Visible = False
Else
'calculate total sum and
Dim newTotal As Double
newTotal = Convert.ToDouble(lblTotalAm.Text) - iOriPrice
lblTotalAm.Text = newTotal
Session.Add("BuyTable", Table)
GridView1.DataSource = Table
GridView1.DataBind()
End If
End Sub
Where does
indexcome from inTable.Rows(index).Delete()?Anyway, this
Delete()only marks the record for deletion in memory. You need to add anAdapter.Update()to delete it from the database.