Musicode
Musicode

Reputation: 661

Windows Forms App Basics: Deleting a row from a bound DataGridView

I have a DataGridView control on a WinForms app that is bound from one of the tables in my database. I would like this DataGridView to change whenever the user "Pulls" a row from the DataGridView to a separate DataGridView located next to it (that is initially blank and not bound to anything).

I'm sure you have seen something like this before, let me draw a diagram. This would be an example of someone who has selected Row1 in the left DataGridView table, and then hit the Pull Button to pull it to the right DataGridView table:

Database:                       ToBeDeleted:
Row1               ______       Row1
Row2               |Pull|
Row3               |--->|                                                ______________
etc..              ------                                               |Remove Records|

Now, when the user "Pulls" over one of the records in the left table to the right table, I would like to left table to update itself to not have that Row in it anymore. This way, the user cannot add two of the same row to the righthand table, and it instantly reflects the changes that they are about to make (since the righthand table represents records that are to be deleted from the lefthand table).

Upvotes: 1

Views: 409

Answers (1)

Vland
Vland

Reputation: 4272

I see nothing wrong about your project and it's not difficult to do. I'm using Linq To Entities for this sample so I have my database table mapped as objects.

enter image description here

  1. Create 2 databound DGVs, bind them to 2 difference datasources (BindingSource) and choose Object. The object type is the same because you're binding them to the same database table (in my case the row object is called as its table, Table3)
  2. Left DGV is bound to Table3BindingSource, right DGV to Table3BindingSourceTBD
  3. Get data for the left DGV from database (a basic select from table)
  4. The button Pull remove an object from left dgv and adds it to the other
  5. The button Delete just loop thru the right rows, and handle the db delete action
  6. Finally, query your db for updated data (left dgv) and clear the right dgv

sample:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    GetData()
End Sub

Sub GetData()
    Dim db = New StackOverflowEntities

    Table3BindingSource.DataSource = db.Table3.OrderBy(Function(x) x.Id).ToList()
End Sub


Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Try
        Dim x = CType(Table3BindingSource.Current, Table3)
        'remove from left dgv
        Table3BindingSource.RemoveCurrent()
        'add to right dgv
        Table3BindingSourceTBD.Add(x)
    Catch ex As Exception
    End Try

End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    'delete rows in datagridview TBD

    Dim db = New StackOverflowEntities
    'loop rows
    For Each r In Table3BindingSourceTBD

        'cast right dgv row to underlying object
        Dim y = CType(r, Table3)

        'set object to be deleted
        Dim dr = (From x In db.Table3
                 Where x.Id = y.Id
                 Select x).Single()

        db.Table3.DeleteObject(dr)
    Next

    Try
        'commit changes to db
        db.SaveChanges()
        MsgBox("db updated")

    Catch ex As Exception

    Finally
        'refresh all data in left dgv
        GetData()
        'simply clear right dgv
        Table3BindingSourceTBD.Clear()
    End Try
End Sub

I used basic Linq To Entities but anything would work (pure SQL, Linq2SQL...). If you need the project source just ask

good luck

Upvotes: 1

Related Questions