-
Mar 21st, 2018, 06:23 AM
#1
Thread Starter
Fanatic Member
[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
-
Mar 21st, 2018, 06:41 AM
#2
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.
-
Mar 21st, 2018, 07:32 AM
#3
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:
For Each view In DataGridView1.SelectedRows. Cast(Of DataGridViewRow)(). Select(Function(dgvr) dgvr.DataBoundItem). ToArray() BindingSource1.Remove(view) 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.
-
Mar 21st, 2018, 09:54 AM
#4
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.
-
Mar 21st, 2018, 08:00 PM
#5
Re: Delete a row from a binding source based on the selected row on a datagridview
Originally Posted by kpmc
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.
-
Mar 21st, 2018, 11:40 PM
#6
Thread Starter
Fanatic Member
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
-
Mar 22nd, 2018, 07:27 AM
#7
Re: [RESOLVED] Delete a row from a binding source based on the selected row on a data
Originally Posted by jmcilhinney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|