VS 2010 Deleting a row from a Data Table using a DGV - only works once-VBForums
Results 1 to 5 of 5

Thread: Deleting a row from a Data Table using a DGV - only works once

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    20

    Question Deleting a row from a Data Table using a DGV - only works once

    I have a data table that is connected to a data grid view. I want to delete the selected row from the data gird view. It works for the first time I delete a row, but when I select another row and try to delete it the program crashes as I get an "Index Out of Row exception". "There is no row at position X"

    Your help would be great!

    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim selectedi As Integer = dgvCustomers.CurrentCell.RowIndex
    
            dtdgvCustomers.Rows(selectedi).Delete()
            Me.Validate()
            Me.objCustomersDA.Update(Me.objDataSet.Tables("tblCustomers"))
            Me.objDataSet.AcceptChanges()
            Refresh()
            dgvCustomers.Update()
    
    
            dgvCustomers.ClearSelection()
    
        End Sub
    
    
    Public Sub Retrieve()
            'Clears DataSet of any existing data
            objDataSet.Clear()
            
    
            objCustomersDA.FillSchema(objDataSet, SchemaType.Source)
            'Fills DataSet with info from DataAdapter
            objCustomersDA.Fill(objDataSet)
    
            'objDataSet.Tables.Add(dtCustomers)
    
    
            dgvCustomers.DataSource = objDataSet.Tables(0)
    
       
    
        End Sub
    Last edited by NewToThis101; Mar 17th, 2013 at 02:41 PM. Reason: spelling

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Deleting a row from a Data Table using a DGV - only works once

    I can see any number of potential problems but the most likely one is the recycling of the data adapter. Don't retain data adapters. Declare and initialise a new adapter in every sub in which you use one. It's slightly overkill but it prevents a whole bunch of inconsistencies such as the one you've now encountered.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,804

    Re: Deleting a row from a Data Table using a DGV - only works once

    This line does nothing in this case:

    Me.objDataSet.AcceptChanges()

    so you can just remove it. Aside from that, I'd put a breakpoint on the second line and make sure that the selectedIndex is something prior to performing the delete. Without seeing the rest of the code we are all just guessing, but it may be that there just isn't a selection anymore.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    20

    Re: Deleting a row from a Data Table using a DGV - only works once

    Hi Shaggy,

    I put in a break point and it is receiving the correct row numbers each time. I don't understand why it won't delete the second time. Any other suggestions?

    I'll try to change the DA as Dunfiddlin suggested but this might get messy.

    Thanks

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,804

    Re: Deleting a row from a Data Table using a DGV - only works once

    I don't actually see you providing a Delete command, but if it deleted even one time then there must be one. I see no particular reason why it would delete only the one time if it is actually deleting. If you step past the delete, then you can look at the datatable and call GetChanges. That should result in a datatable that has only a single row with RowState = deleted. That might be worth a test.

    I've reused dataadapters before without any problem, so I'm not convinced that's where the problem lies. Still, it doesn't look all that bad, so it may prove to be a rather obscure issue.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.