Deleting a row from a DataGridView???
I have a datagridview that I am trying remove a row. The grid is bound to a datatable and I'm deleting the row from the datatable...problem is I'm not sure how to reference the row in the grid. Here's my statement:
dtTable.Rows.Remove(grd.CurrentRow.?)
The ? is...what's next?
Thanks,
Re: Deleting a row from a DataGridView???
That is not necessary. If the grid is bound to the DataTable then deleting the row from the DataTable will automatically remove it from the grid. That's the whole point of data binding: changes to the data source will be reflected in the bound control and vice versa.
Re: Deleting a row from a DataGridView???
I know...but I'm not sure how to reference the current row of the grid which is just a pointer the the Dataset row.
Re: Deleting a row from a DataGridView???
Can anyone tell me how to delete a selected row from a bound DataGridView?
I have a datatable that is bound to a grid. I just want to remove a row from the datatable.
Thanks,
Re: Deleting a row from a DataGridView???
As JMC said, "That's the whole point of data binding: changes to the data source will be reflected in the bound control and vice versa."
So you can either remove rows form the datagridview or from the bound datatable itself..
To remove selected rows from the datagridview:
VB Code:
For i As Integer = DataGridView1.SelectedRows.Count - 1 To 0 Step -1
DataGridView1.Rows.Remove(DataGridView1.SelectedRows(i))
Next
And to remove selected rows from the table directly
VB Code:
Dim row As DataGridViewRow
For i As Integer = DataGridView1.SelectedRows.Count - 1 To 0 Step -1
row = DataGridView1.SelectedRows(i)
dt.Rows.RemoveAt(row.Index)
Next
The trick is looping backward on the datagridview's selectedrows collection
Re: Deleting a row from a DataGridView???
Stan,
I'm using the code to delete from the Datatable but it doesn't seem to be working. I'm selecting only 1 row from my datagrid and it's Row 0. It goes thru the loop fine but it doesn't remove the row from the grid. Do I need to refresh my grid after the statement?
Re: Deleting a row from a DataGridView???
No, you don't have to refresh the datagridview at all... My question is, did you select a row entirely (by clicking on its row header) or did you just select a cell in a row?
Re: Deleting a row from a DataGridView???
also make sure the AllowUserToDeleteRows property is set to true ;)
Re: Deleting a row from a DataGridView???
Kleinma,
That was something that I didn't have set, however, it's still not deleting from the grid. I checked the datatable count and it shows that the row was removed but it's still not removing it from the bound datagridview. Is it different when you remove it from a datatable as opposed to a dataset? If that's the case then I'm not deleting it from the dataset. Here is the code that is supposed to remove it from the grid.
VB Code:
Dim row As DataGridViewRow
For i As Integer = grd.SelectedRows.Count - 1 To 0 Step -1
row = grd.SelectedRows(i)
dtConsignees.Rows.RemoveAt(row.Index)
Next
Thanks,
Re: Deleting a row from a DataGridView???
Removing a row and deleting the record are NOT the same thing:
VB Code:
DirectCast(myDataGridView.CurrentRow.DataBoundItem, DataRowView).Delete()
or for multiple selected rows this should work:
VB Code:
For i As Integer = myDataGridView.SelectedRows.Count - 1 To 0 Step -1
DirectCast(myDataGridView.SelectedRows(i).DataBoundItem, DataRowView).Delete()
Next i
Re: Deleting a row from a DataGridView???