|
-
Jan 27th, 2007, 08:01 PM
#1
Thread Starter
PowerPoster
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,
-
Jan 27th, 2007, 09:28 PM
#2
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.
-
Jan 29th, 2007, 10:16 AM
#3
Thread Starter
PowerPoster
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.
-
Jan 29th, 2007, 10:53 AM
#4
Thread Starter
PowerPoster
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,
-
Jan 29th, 2007, 12:24 PM
#5
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
-
Jan 29th, 2007, 01:19 PM
#6
Thread Starter
PowerPoster
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?
-
Jan 29th, 2007, 02:27 PM
#7
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?
-
Jan 29th, 2007, 02:29 PM
#8
Re: Deleting a row from a DataGridView???
also make sure the AllowUserToDeleteRows property is set to true
-
Jan 29th, 2007, 03:06 PM
#9
Thread Starter
PowerPoster
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,
-
Jan 29th, 2007, 03:44 PM
#10
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
-
Jan 29th, 2007, 04:09 PM
#11
Thread Starter
PowerPoster
Re: Deleting a row from a DataGridView???
Last edited by blakemckenna; Jan 29th, 2007 at 04:13 PM.
Blake
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
|