Click here to Skip to main content
15,891,757 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
Updated 11-Dec-10 7:49am
v2

Why are you using "dt". It is a locally declared variable. Isn't it?
VB
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
 
Share this answer
 
v3
Comments
edriso 13-Dec-10 14:22pm    
hii 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
Prerak Patel 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 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 15-Dec-10 14:30pm    
for some reason it works now thanks for you all
mungalim 20-Aug-15 9:55am    
thank you very helpful solutions
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
 
Share this answer
 
v2
Comments
Toniyo Jackson 13-Dec-10 1:35am    
Always use code block for code.
edriso 13-Dec-10 14:27pm    
it removes all the rows not the selected rows only
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?
 
Share this answer
 
Comments
edriso 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
Dr.Walt Fair, PE 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.
edriso 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
Dr.Walt Fair, PE 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900