DataGridView - Delete Selected Row
DataGridView - Delete Selected Row
DataGridView - Delete Selected Row
DataGridView - Delete Selected Row
DataGridView - Delete Selected Row
DataGridView - Delete Selected Row DataGridView - Delete Selected Row DataGridView - Delete Selected Row DataGridView - Delete Selected Row DataGridView - Delete Selected Row DataGridView - Delete Selected Row DataGridView - Delete Selected Row DataGridView - Delete Selected Row
DataGridView - Delete Selected Row DataGridView - Delete Selected Row
DataGridView - Delete Selected Row
Go Back  Xtreme Visual Basic Talk > > > DataGridView - Delete Selected Row


Reply
 
Thread Tools Display Modes
  #1  
Old 09-16-2006, 11:45 AM
LauraL LauraL is offline
Regular
 
Join Date: Jun 2005
Posts: 97
Question DataGridView - Delete Selected Row


Hi all,

I have the following problem. I need to delete data from my access database through DataGridView. I have fullrowselection enabled. How could I delete the selected row from database. My code now looks like this:


Code:
 For Each MyRow As DataRow In MyDatabase.Tables("Products").Rows
            If (MyRow("ProductCode")) = "255445" Then
                MyRow.Delete()
            End If
         Next

        MyDatabase.Tables("Products").AcceptChanges()
I think I should somehow replace the productcode "255455" with DataGridView.Selection...or something?

I cannot figure it out. Please help!
Laura
Reply With Quote
  #2  
Old 09-17-2006, 01:47 AM
LauraL LauraL is offline
Regular
 
Join Date: Jun 2005
Posts: 97
Default

Does'nt anybody know the answer???
Reply With Quote
  #3  
Old 09-18-2006, 10:36 AM
Aquila's Avatar
Aquila Aquila is offline
Contributor
 
Join Date: Oct 2002
Location: Ocala, Fl.
Posts: 597
Default

Quote:
Originally Posted by LauraL
Hi all,

I have the following problem. I need to delete data from my access database through DataGridView. I have fullrowselection enabled. How could I delete the selected row from database. My code now looks like this:


Code:
 For Each MyRow As DataRow In MyDatabase.Tables("Products").Rows
            If (MyRow("ProductCode")) = "255445" Then
                MyRow.Delete()
            End If
         Next

        MyDatabase.Tables("Products").AcceptChanges()
I think I should somehow replace the productcode "255455" with DataGridView.Selection...or something?

I cannot figure it out. Please help!
Laura
You need to updated the datasource first.

Code:
DataAdapter.Update(Datasource.TableName) Datasource.tablename.acceptchanges

Sorry it took so long to answer this, I was looking for something else about DataGridViews when i ran accross this and noticed it wasn't answered.

Replace DataAdapter with the name of your own dataadapter and the datasource as well.
__________________
Plotting to take over the world since 1978
Reply With Quote
  #4  
Old 09-18-2006, 11:01 AM
wayneph's Avatar
waynephDataGridView - Delete Selected Row wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

You may have also gotten a faster answer if you had posted in the .NET section as opposed to Legacy VB. DataGridViews and DataSets don't mean anything up there...
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #5  
Old 09-18-2006, 03:46 PM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Oct 2004
Location: Montréal
Posts: 1,135
Default

If I understand you correctly, you want to delete rows selected by a user from your DGV.

1. Use the DataGridViewRowCollection of your DGV rather than the DataRowCollection of the DataTable. The DataGridViewRow has the Selected property that indicates whether a row is selected or otherwise.

2. Once you have determined that a row is to be deleted, you can use the Remove method of the DataGridViewRowCollection to delete the item from the grid, e.g. YerDataGridView.Rows.Remove(row)

3. Note that at this point, although the item is removed from the DGV, it still has not been deleted from the Access DB. You need to call the TableAdapter Update method on your DataSet/DataTable to commit the deletions to the DB, e.g. YerTableAdapter.Update(YerDataSet)

I normally would call Update once to commit the changes only after having removed all the items to be deleted from the DGV.

Last edited by IUnknown; 09-18-2006 at 03:55 PM.
Reply With Quote
  #6  
Old 09-20-2006, 01:08 AM
LauraL LauraL is offline
Regular
 
Join Date: Jun 2005
Posts: 97
Default

Thank you IUnknows, you have lead me to the right way and it works now.
Happy Laura
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
DataGridView - Delete Selected Row
DataGridView - Delete Selected Row
DataGridView - Delete Selected Row DataGridView - Delete Selected Row
DataGridView - Delete Selected Row
DataGridView - Delete Selected Row
DataGridView - Delete Selected Row DataGridView - Delete Selected Row DataGridView - Delete Selected Row DataGridView - Delete Selected Row DataGridView - Delete Selected Row DataGridView - Delete Selected Row DataGridView - Delete Selected Row
DataGridView - Delete Selected Row
DataGridView - Delete Selected Row
 
DataGridView - Delete Selected Row
DataGridView - Delete Selected Row
 
-->