Results 1 to 6 of 6

Thread: [RESOLVED] delete rows from datagridview

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    Resolved [RESOLVED] delete rows from datagridview

    I have been looking at information to delete a selected row from a datagridview. I wanted to do it with a double click in a row of the displayed tables. I found the method I am using here. I have no idea whether a EndEdit() and Update() are required, but I figured it couldn't hurt to put them in the code.
    So I tested it and when I double clicked in a row the row was removed....... but was it really? No it was not. So the question is, what am I missing to close the deal on this and make the row permanently go away?
    I also tested it with and withoug the EndEdit() and Update().

    Code:
        Private Sub dgvVendor_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvVendor.CellDoubleClick
                    dgvVendor.Rows.RemoveAt(e.RowIndex)
                    Me.lnkItemVendorBindingSource.EndEdit()
                    Me.LnkItemVendorTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: delete rows from datagridview

    Get rid of the RemoveAt and EndEdit calls and instead call RemoveCurrent on the BindingSource. That will result in a call to the Delete method of that DataRow, where removing the row from the grid has no effect on the data source.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    Re: delete rows from datagridview

    Actually, I already made a stab at the RemoveCurrent and got the following error:

    Name:  error.jpg
Views: 1555
Size:  19.7 KB

    It is apparent to me that the double click event to select a record is not actually selected. It appears to me that I need something more that points to the record to make that work. And as far as the RemoveAt I get the idea that there is no way to close the deal and make it actually happen.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: delete rows from datagridview

    What is SelectionMode of the grid set to? Normally a double-click in a cell should select that row but it appears that that is not the case for you, which would have to be due to the SelectionMode, I would think. If you don't want to change that, you should be able to call RemoveAt on the BindingSource and use e.RowIndex as the argument. That will have the same effect. After a bit of error checking, RemoveCurrent simply calls RemoveAt and passes Position as the argument.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    Re: delete rows from datagridview

    SelectionMode = RowHeaderSelect

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    705

    Re: delete rows from datagridview

    JM, I have to tell you now that I might have given you some bad information last night on this (not might, I did). To be fair, it was really, really late and my mind wasn't at its best. So hear is the deal. What I am trying to do is delete a row in one table based on information from another table. That may sound confusing, but let me explain.

    The table that the DGV is bound to is not the table that I want to delete the row from. This table is related to another table by a join to a third table (I fail to remember what that type of relation is called). Only two of the three tables are involved in this transaction. Anyway, the query displays the rows in the DGV uses tblSupplierMaster and the table I want to delete the row from is lnkItemVendor. Long story short is when we were using the RemoveCurrent there was two problems. So first let me show you the query and the code to gets the job done.

    Code:
    SELECT        tblSupplierMaster.intSiTechID, tblSupplierMaster.intChangeID, tblSupplierMaster.strVendorName, tblItemMaster.intSiTechID AS Expr1
    FROM            ((lnkItemVendor INNER JOIN
                             tblItemMaster ON tblItemMaster.intSiTechID = lnkItemVendor.intSiTechItemID) INNER JOIN
                             tblSupplierMaster ON tblSupplierMaster.intSiTechID = lnkItemVendor.intSiTechVendorID)
    WHERE        (lnkItemVendor.intSiTechItemID = ?)
    So the first mistake I made was that tblSupplierMaster.intChangeID was not included in the SELECT. It never had a chance without that.


    Code:
        Private Sub dgvVendor_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvVendor.CellDoubleClick
                    _intSiTechSourceID = CInt(Me.dgvVendor.Rows(e.RowIndex).Cells(0).Value.ToString)
                    _IntChangeID = CInt(Me.dgvVendor.Rows(e.RowIndex).Cells(1).Value.ToString)
                    Me.LnkItemVendorTableAdapter.FillByDeleteVendor(Me._MasterBase4_0ItemMasterDataSet.lnkItemVendor, _IntChangeID, _intSiTechSourceID)
                    lnkItemVendorBindingSource.RemoveCurrent()
                    Me.LnkItemVendorTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet.lnkItemVendor)
                    With dgvVendor
                        .Enabled = False
                    End With
                    SetLoadProperties()
            End Select
    The second problem is caused by the first. Since the SELECT in the Query was missing a needed field then there was nothing for _intChangeID and the code failed.

    Of course last night I wasn't even that close. I was thinking that the DGV was displaying from the join table lnkItemVendor (still can't remember what that is called) instead of tblSupplierMaster.

    At any rate, once I removed my head from my butt and got a breath of fresh air this morning I implemented the code above, using the RemoveCurrent, as you suggested, and it works.

    Thanks for actually providing the right information when you were provided with bad information from me.

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
  •  



Click Here to Expand Forum to Full Width