Deleting a row in a datagrid

  • Thread starter NDady via DotNetMonster.com
  • Start date
N

NDady via DotNetMonster.com

Hi,
I have a datagrid populated from a dataset. On every row in the grid I have
a delete button. When the user presses on the delete button I remove the
row from the dataset and rebind the datagrid.
The problem is that after a couple of delete the index in the dataset does
not match the index in the grid and the wrong record i deleted from the
dataset. How can I solve this Problem? I am using the following procedure:

Private Sub dg1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg1.ItemCommand
If e.CommandName.ToString = "Delete" Then
Dim dset As DataSet
dset = CType(Session("VideoDset"), DataSet)
dset.Tables(0).Rows(e.Item.ItemIndex()).Delete()

dg1.DataSource = dset
Session("videoDset") = dset
dg1.DataBind()
End If
End Sub

Thanks
NDady
 
M

msnews.microsoft.com

NDady:
I'm not 100% sure, but I think it's because your Rows aren't actually
deleted, simply marked for deletion. Let me explain, let's say you have 4
rows, at first it'll look like this:

Grid-Row-0 == DataSet-Row-0
Grid-Row-1 == DataSet-Row-1
Grid-Row-2 == DataSet-Row-2
Grid-Row-3 == DataSet-Row-3

So when you get e.Item.ItemIndex the first time, it'll delete the correct
record in the dataset. However, when you rebind, thinks fall out of synch.
Let's say Row 1 was deleted, it'll now look like:

Grid-Row-0 == DataSet-Row-0
Grid-Row-1 == DataSet-Row-2
Grid-Row-2 == DataSet-Row-3

You see, DataSet-Row-1 still exist, but it isn't bound/displayed. So when
you delete Grid-Row-1, you are getting index "1", and deleting data row "1".

What I would do is place a CommandArgument in the bottom which is the unique
Id of the row and access the value from e.CommandArgument.

Karl
 
N

NDady via DotNetMonster.com

Hi Karl,
How can I implement your solution in my code. You see I am quite new in
..Net.
Is there any way to re-index the dataset so it will look like the datagrid?

Regards
NDady
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top