PDA

Click to See Complete Forum and Search --> : Delete row


sara_22
Jun 29th, 2005, 02:24 AM
Hello
i have the dataset (data) which contains numbers of rows and
these rows are displayed in datagrid
i added delete command coloum in the datagrid
and i want when the user click the delete button
the associated row is deleted from the dataset and from the database

the code is:

Dim id As Integer
id = e.Item.ItemIndex
Dim strdel As String = "delete from AllAlumni where AllAlumniNum=" & id
Dim sqlcommand As SqlCommand = New SqlCommand(strdel, cnn)
Dim adapt As New SqlDataAdapter
adapt.UpdateCommand = sqlcommand
adapt.Update(data, "AllAllumni")
it doesn't work

thanks
sara

mendhak
Jun 29th, 2005, 03:50 AM
Is "data" your dataset?

data.Tables(0).Rows(e.Item.ItemIndex).Delete()

You can then perform your dataadapter's UPDATE().

sara_22
Jul 1st, 2005, 03:40 AM
Dear mendhak
Yes, Data is the dataset
I did this command

data.Tables(0).Rows(e.Item.ItemIndex).Delete()

but the row is deleted from the dataset only , the database is never changed

mendhak
Jul 1st, 2005, 04:07 AM
Did you update?

sara_22
Jul 1st, 2005, 04:16 AM
Ya
using the following command
adapt.UpdateCommand = sqlcommand
adapt.Update(data, "AllAllumni")

mendhak
Jul 1st, 2005, 04:25 AM
adapt.DeleteCommand = sqlcommand

jarenken
Jul 11th, 2005, 11:20 PM
normally when i need to delete a record using a datagrid, i store the pk id in a hidden column. then on the itemcommand i search the dataset for it, delete the row, update, and then rebind. this will guarantee that you have the right record.

row = ds.tables(0).rows.find(e.item.cells(0).text)
row.delete
da.update(ds)
ds.acceptchanges
dgr.datasource = ds
dgr.databind