Results 1 to 7 of 7

Thread: [RESOLVED] Delete a row from a binding source based on the selected row on a datagridview

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Resolved [RESOLVED] Delete a row from a binding source based on the selected row on a datagridview

    I have a datagridview that im trying to delete a row from but i need it to update the main source which is a DataTable.

    the links of this table is as follows.... Datatable > BindingSource > DataGridView

    what i want is that when i select a row on the DGV, i can find the row on the DataTable to remove it. is there a way of doing this without looping through the DT to find matching columns?
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Delete a row from a binding source based on the selected row on a datagridview

    There are several ways to do that. one is to have the record id in a hidden column of the grid, another is to Access the .DataBoundItem property (i think this is what ist called) of the datagridviewrow, this would give you a datarow or datarowview.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Delete a row from a binding source based on the selected row on a datagridview

    What exactly do you mean when you say "selected row"? People often use that expression incorrectly. In a DataGridView, a selected row is one that is highlighted. There can be zero, one or more rows selected at a time and those rows are stored in the SelectedRows collection. If you actually mean the row that currently contains the caret then you actually mean the current row. There can never be more than one current row and there will always be one current row unless there are no rows in the grid. The current row is, not surprisingly, stored in the CurrentRow property.

    So, if you actually mean the current row then the solution could not be simpler. You just have to call RemoveCurrent on the BindingSource. If you really do mean the selected rows then you can do this:
    vb.net Code:
    1. For Each view In DataGridView1.SelectedRows.
    2.                                Cast(Of DataGridViewRow)().
    3.                                Select(Function(dgvr) dgvr.DataBoundItem).
    4.                                ToArray()
    5.     BindingSource1.Remove(view)
    6. Next
    In that case, 'view' will be an Object reference to a DataRowView object. The reason that you need to call ToArray is that the Remove call will implicitly call Delete on the DataRowView and that will cause the DataGridViewRow to be removed from the grid, which would cause the SelectedRows collection to change. If we were enumerating the SelectedRows collection directly, that would be an issue, i.e. you can't change a collection while enumerating it. If we have already retrieved the bound DataRowViews and populated an array with them then that array will not be changed by the deletions.

  4. #4
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Delete a row from a binding source based on the selected row on a datagridview

    Cant you just use
    Code:
    BindingSource.RemoveCurrent
    Last edited by kpmc; Mar 21st, 2018 at 10:11 AM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Delete a row from a binding source based on the selected row on a datagridview

    Quote Originally Posted by kpmc View Post
    Cant you just use
    Code:
    BindingSource.RemoveCurrent
    You can if you're actually talking about the current row, which is why I suggested it. The OP actually talked about a selected row though, which may or may not mean the current row.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Delete a row from a binding source based on the selected row on a datagridview

    Thanks guys.....

    Yes i was actually selecting the whole row and did it that way in case i wanted to select several items but i may just opt for the current Row option as its very simple , all im doing here is transferring items from one table to another and deleting the transferred one after.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  7. #7
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: [RESOLVED] Delete a row from a binding source based on the selected row on a data

    Quote Originally Posted by jmcilhinney View Post
    You can if you're actually talking about the current row, which is why I suggested it. The OP actually talked about a selected row though, which may or may not mean the current row.
    Now that I read you more carefully I see that you've mentioned that.

    I dont like to use DGV for anything other than showing data. I helped a person do something similar recently but with dataview.rowfilter approach.
    For OP purpose I would do more like this
    Code:
    Public Class MoveRows
        Dim Dset As New DataSet
        Dim BS1, BS2 As New BindingSource
        Private Sub MoveRows_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dset.Tables.Add(New DataTable With {.TableName = "T1"})
    
            With Dset.Tables("T1").Columns
                .Add("Selected", GetType(Boolean))
                .Add("Col1", GetType(String))
                .Add("Col2", GetType(String))
            End With
            Dim dt As DataTable = Dset.Tables("T1").Clone
            dt.TableName = "T2"
            Dset.Tables.Add(dt)
            For i As Integer = 0 To 25
                Dset.Tables("T1").Rows.Add(False, "Col1Val" & i, "Col2Val" & i)
            Next
    
            BS1.DataSource = Dset.Tables("T1")
            DataGridView1.DataSource = BS1
            BS2.DataSource = Dset.Tables("T2")
            DataGridView2.DataSource = BS2
    
        End Sub
    
        Private Sub ButtonMoveRow_Click(sender As Object, e As EventArgs) Handles ButtonMoveRow.Click
            For Each Drow As DataRow In Dset.Tables("T1").Rows
                If Convert.ToBoolean(Drow("Selected")) Then
                    Dset.Tables("T2").ImportRow(Drow)
                End If
            Next
            For Each DRV As DataRowView In BS1
                If Convert.ToBoolean(DRV("Selected")) Then
                    BS1.Remove(DRV)
                End If
            Next
        End Sub
    End Class

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