Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hi,
 
I am new to vb.net 2008. In a datagridview I added a column with checkboxes. I need a code to delete the checked rows only.
Many thanks.
Posted 11-Dec-10 8:44am
edriso403
Edited 11-Dec-10 8:49am
JF201553.3K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Why are you using "dt". It is a locally declared variable. Isn't it?
For x As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
  If DataGridView1.Rows(x).Cells("checked").Value Then
    DataGridView1.Rows.Remove(DataGridView1.Rows(x))
  End If
Next
  Permalink  
v3
Comments
edriso at 13-Dec-10 14:22pm
   
hi
i try ur code but i got this exception message,(Column named select cannot be found. Parameter name: columnName)
the checkboxes column header name is (select).
For x As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
If DataGridView1.Rows(x).Cells("select").Value Then
DataGridView1.Rows.Remove(DataGridView1.Rows(x))
End If
Next
prerakpatel at 14-Dec-10 2:23am
   
Are you sure?! I think "select" is an identifier, so you can't set it as name. Try some other name.
edriso at 15-Dec-10 14:16pm
   
i have changed the column name and still gives the same error any idea why? please don't give up
thanks
edriso at 15-Dec-10 14:30pm
   
for some reason it works now thanks for you all
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
To delete The checked Rows,
 
For example dtTable is the datatable that bind gridview.
gridview1.datasource=dtTable.
First find the Checked item in the table,
For i=0 to dtTable.Rows.Count-1
 
      if dtTable.Rows(i).Item("Checked")=true then
          gridview1.rows.removeat(i)
     end if
next
 

Regards
Sreyas MN
  Permalink  
v2
Comments
ToniyoJackson at 13-Dec-10 1:35am
   
Always use code block for code.
edriso at 13-Dec-10 14:27pm
   
it removes all the rows not the selected rows only
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Basically you can loop through the rows and delete the ones that have the checkbox checked. If you go by index numbers, start from the end so the ones you haven't processed don't change position.
 
What have you tried so far that doesn't work?
  Permalink  
Comments
edriso at 11-Dec-10 16:55pm
   
i did try this one but still not working, any idea
 
Dim x As Integer
For x = (DataGridView1.Rows.Count - 1) To 0
If DataGridView1.Rows(x).Cells(1).Value = False Then
DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
End If
Next
Walt Fair, Jr. at 11-Dec-10 15:48pm
   
For one thing, you probably only want to loop from (Rows.Count - 1).
 
First, you'll need to go backwards through the loop: For x = (dt1.Rows.Count - 1) to 0 because if you delete a Row the index of all the rows after may change.
 
To determine if a check box is checked, use something like (N is the column number of your CheckBox column)
If dt.Rows(x).Cells(N).Value = True Then
' Do something
End If
 
Finally, you may need to refresh the grid control afterward, so it displays the updated rows properly.
Walt Fair, Jr. at 11-Dec-10 23:26pm
   
Try debugging and stepping through the loop. You probably need to specify Step -1 on the For loop or it won't actually run, since the default Step is 1 and x will increment, instead of decrement.
edriso at 11-Dec-10 15:27pm
   
here is my code, but it is not working!
 
Dim x As Integer
Dim dt1 As DataTable
Dim row As DataGridViewRow
 
For x = 0 To dt1.Rows.Count
If dt.Rows(1).Item("checked") = False Then
DataGridView1.Rows.Remove(row)
End If
Next

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 165
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 13 Dec 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100