[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how do i delete rows in a datagridview based on the checkbox condition in vb.net 2008

Posted on 2009-04-24
5
Medium Priority
?
577 Views
Last Modified: 2012-05-06
hi, i am having a table in the datagridview with six columns. the last column is a checkbox type. I want to delete the rows from the table with the checkbox ticked on the click of a button. Since i am a beginner with vb.net, I would greatly appreciate any help in this in the form of the syntax format.
0
Comment
Question by:moonis_ahmed
  • 3
5 Comments
 
LVL 9

Expert Comment

by:Deathrace
ID: 24224785
hey,, is this web app or windows ?
0
 

Author Comment

by:moonis_ahmed
ID: 24226734
This is a windows application.
0
 
LVL 33

Accepted Solution

by:
jppinto earned 1000 total points
ID: 24233398
Try something like this:

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      For Each m_row as System.Windows.Forms.DataGridViewRow in me.DataGridView1.Rows
         If m_row.Cells("Column1").Value=True Then    
           Me.DataGridView1.Rows.Remove(m_row)
        End If
    Next
End Sub

Column1 is the column where you have the CheckBox. This code only deletes the row from the DataGridView. If the DataGridView is bound to a table and you want to remove the record from the table check this post:

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22130641.html?sfQueryTermInfo=1+10+bound+datagridview+delet+row

Hope this helps you,

jppinto
0
 

Author Comment

by:moonis_ahmed
ID: 24236383
Hi,

I got a weird problem now with this operation. Let's say I have 3 data rows in the datagridview. If i select the first or the last row to delete, it deletes them properly. however, if i select any middle row other than the ones on the ends, the remove operations deletes all the rows except the row selected to be deleted.

Also, once i delete a row, how do i update the datagridview to reflect the change?


Private Sub RemoveSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim response As DialogResult = MessageBox.Show("Are you sure you want to delete the selected ticket(s)?", "Delete row?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        If (response = DialogResult.No) Then
            Exit Sub
        Else
            For Each m_row As System.Windows.Forms.DataGridViewRow In Me.DataGridView1.Rows
                If m_row.Cells("Column6").Value = True Then
                    Me.DataGridView1.Rows.Remove(m_row)
                End If
            Next
        End If
    End Sub

Open in new window

0
 

Author Comment

by:moonis_ahmed
ID: 24236542
Ignore the above comment, I got it working. I was referencing the wrong column number!

My mistake!

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Loops Section Overview
Suggested Courses

831 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