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

Posted on 2009-04-24
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, I would greatly appreciate any help in this in the form of the syntax format.
Question by:moonis_ahmed
    LVL 9

    Expert Comment

    hey,, is this web app or windows ?

    Author Comment

    This is a windows application.
    LVL 33

    Accepted Solution

    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    
            End If
    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:

    Hope this helps you,


    Author Comment


    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
                For Each m_row As System.Windows.Forms.DataGridViewRow In Me.DataGridView1.Rows
                    If m_row.Cells("Column6").Value = True Then
                    End If
            End If
        End Sub

    Open in new window


    Author Comment

    Ignore the above comment, I got it working. I was referencing the wrong column number!

    My mistake!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now