Solved

How do I delete a row in a DataGridView?

Posted on 2007-11-28
5
651 Views
Last Modified: 2013-11-26
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
Comment
Question by:grouchyfd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
Solar_Flare earned 500 total points
ID: 20369557
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
 

Author Comment

by:grouchyfd
ID: 20370313
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
 
LVL 5

Expert Comment

by:ursangel
ID: 20371701
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
 

Author Comment

by:grouchyfd
ID: 20375621
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
 
LVL 15

Expert Comment

by:Solar_Flare
ID: 20378259
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question