Results 1 to 7 of 7

Thread: Delete row

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    21

    Delete row

    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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Delete row

    Is "data" your dataset?

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

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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    21

    Re: Delete row

    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

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Delete row

    Did you update?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    21

    Re: Delete row

    Ya
    using the following command
    adapt.UpdateCommand = sqlcommand
    adapt.Update(data, "AllAllumni")

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Delete row

    adapt.DeleteCommand = sqlcommand

  7. #7
    Junior Member
    Join Date
    Jun 2002
    Location
    Midwest
    Posts
    27

    Re: Delete row

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width