DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows
Go Back  Xtreme Visual Basic Talk > > > DataGridView: Remove Selected Rows


Reply
 
Thread Tools Display Modes
  #1  
Old 10-01-2007, 04:44 PM
Neogortex Neogortex is offline
Newcomer
 
Join Date: Oct 2007
Posts: 1
Default DataGridView: Remove Selected Rows


The code below will allow for one selected DataGridView Row to be deleted at a time.. I need to be able to delete multiple selected Rows at one click of the Remove Button; sounds simple but has proven to be a great pain.

Thanks, In Advance, For Any Help!!

VB.NET 2005
.NET Framework 2.0


Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnTest.Click

Dim n As Integer = 0

For Each dgvRow As DataGridViewRow In DataGridView1.Rows

If CBool(DataGridView1.Rows(n).Cells("CheckBox").Value = False Then

n += 1

ElseIf CBool(DataGridView1.Rows(n).Cells("CheckBox").Value) = True Then

DataGridView1.Rows(n).Cells("CheckBox").DataGridView.Rows _
.Remove(DataGridView1.CurrentRow)

n += 1

End If

Next

End Sub

Last edited by Neogortex; 10-01-2007 at 04:53 PM. Reason: Make the thread more understandable
Reply With Quote
  #2  
Old 10-01-2007, 05:05 PM
IUnknown's Avatar
IUnknown IUnknown is offline
Senior Contributor

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

Hi and welcome to the forum.

If you want to delete rows based on the value in a cell, and as you're using the For Each ... Next loop, you can first mark the rows you want to delete as Selected when the condition if met, viz
Code:
'In the first For Each loop: If CBool(dgvRow.Cells(3).Value) Then dgvRow.Selected = True Else dgvRow.Selected = False End If 'Or just dgvRow.Selected = CBool(dgvRow.Cells(3).Value)
Next you can remove all the selected rows
Code:
For Each selectedrow As DataGridViewRow In DataGridView1.SelectedRows DataGridView1.Rows.Remove(selectedrow) Next
__________________
win7 : vs 2008 : .Net 3.5

Last edited by IUnknown; 10-01-2007 at 07:18 PM. Reason: Added information and corrected errors
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: Remove Selected Rows
DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows
 
DataGridView: Remove Selected Rows
DataGridView: Remove Selected Rows
 
-->