Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 669
  • Last Modified:

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
0
grouchyfd
Asked:
grouchyfd
  • 2
  • 2
1 Solution
 
Solar_FlareCommented:
if the datagridview is databound then you will need to remove the row from the underlying data source rather than trying to remove the actual datagridviewrow from the control.
0
 
grouchyfdAuthor Commented:
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.
0
 
ursangelCommented:
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.
0
 
grouchyfdAuthor Commented:
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.
0
 
Solar_FlareCommented:
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)
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now