Link to home
Start Free TrialLog in
Avatar of grouchyfd
grouchyfdFlag for United States of America

asked on

How do I delete a row in a DataGridView?

I have an ADO.Net app. In the app is an order form that is a master-detail form, for each order number, there is a datgridview where the user can select an item and qty then select the next row and add another item and qty. The datagrid can have multiple rows. How do I allow the user to delete a row if he/she determines they do not want that item any more, before the order is saved (and the table updated)? I have a button but I just can't seem to get the code to make it remove a highlighted row.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Solar_Flare
Solar_Flare

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of grouchyfd

ASKER

The data is not committed to the database yet, it's just stored in a dataset. I would like the user to be able to remove the selected or highlighted row from the dataset.
Avatar of ursangel
ursangel

If the datagrid view is binded with a data source, all you have to do is to set the DataRowState to deleted. the item will be deleted once its posted to the
 database.
Solar Flare
Thanks. I found that if I update the table and then delete the row with a DELETE command in the same sub, this works. I was hoping for a way to delete the row strait from the dataset before it was committed to the database but this still works fine.
you can remove the row from the dataset entirely

this is the idea, you can use the table's row collection's .Remove method



dim row as datarow = datagridview.selectedrows(0).Databounditem.row

dataset1.tables(0).rows.Remove(row)