Datagrid Delete

Patty05

Active member
Joined
Aug 28, 2006
Messages
29
Programming Experience
Beginner
I have added and populated a datagrid adding a dataadapter to the form and making the connection and creating the dataset etc. When the form loads, the datagrid generates properly, and I can edit a field by clicking the field in the datagrid and type it in and I can add a new row by clicking the * at the bottom of the datagrid. I have a save button that saves the changes by using this code:

VB.NET:
[SIZE=2]OleDbDataAdapter1.Update(EmpInfoDataSet1)
[/SIZE]

However, I can't delete an entire row within the datagrid. It seems I should be able do that right within the datagrid itself just as I can edit and add a row.

Is there a property or something I need to set to be able to delete a row within the datagrid control?
 
You don't actually delete a row from the datagrid per se. A datagrid just displays data from the underlying table. So what you need to do is find the row in the datatable you want to delete. The easiest way, if you know the primary key value of the row you want to delete, is to use the find method of the datatable...

VB.NET:
Dim Dr as datarow = Datatable.Rows.Find(primary key value)
 
Dr.Delete.
 
if I wanted to delete a single row that I have been selecting...this will be useful to me ......
VB.NET:
Dim rowind As Integer = Me.DataGrid1.CurrentRowIndex 
Me.myDataTable.Rows.RemoveAt(rowind)
 
Last edited by a moderator:
Back
Top