dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Removing Rows from DataGridView

  1. #1
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Removing Rows from DataGridView

    I need to remove certain rows from a DataGridView based on whether the row contains a ! or not. The reason for this is that I don't need rows that contain a ! to be saved to the database. The code I have so far is:
    Code:
    Public Function RemoveEmptyRows()
            Dim DataGridView1 As New DataGridView
            For Each row As DataGridViewRow In DataGridView1.Rows
                If row.Cells("Description").Value = "!" Then
                          DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
                    End If
                Else
                End If
            Next
        End Function
    This doesn't seem to remove any row though.

    It's probably something simple but my mind is not what it should be today!
    Visual Basic 2005 ver. 8.0.50727.867

  2. #2
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Removing Rows from DataGridView

    where are those empty records come from? anyway, instead of for-each try to use an index in a loop, don't increment the index if a record has been deleted inside the loop..

    Code:
            dim i as integer = 0
            while i < DataGridView1.Rows.Count 
               if dataGridview1.rows(i).cells("Description").value = "!" then
                  dataGRidview1.rows.removeAt(i)
               else
                  i += 1
               end if
            wend
    Busy

  3. #3
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Re: Removing Rows from DataGridView

    Hi Thread1 Thanks for the reply!

    To answer your question, some of the rows will have some of the cells empty because it is basically a form that Teachers fill in to inform Senior Management Team about incidents that have happened in class between students (fights etc). The data that is in the DataGridView comes from an Access Database and includes Student ID, Name etc based on which Class the Teacher is teaching at that particular time.

    The info is then stored in a Spreadsheet (because lots of mathamatical statistics need to be produced from it) and accessed by Senior Management Team members.

    Will give you code a go and let you know how I get on.

    Cheers
    Visual Basic 2005 ver. 8.0.50727.867

  4. #4
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Re: Removing Rows from DataGridView

    Thread1

    Just tried the code but it doesn't work. No error, just no rows deleted.

    Any idea where I am going wrong?

    Thanks
    Last edited by brjames32; December 9th, 2008 at 08:13 AM.
    Visual Basic 2005 ver. 8.0.50727.867

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,267

    Re: Removing Rows from DataGridView

    Great code Thread!

    This code has worked on my pc, have you tried FormattedValue instead of Value like :
    Code:
     if dataGridview1.rows(i).cells("Description").Formattedvalue = "!" then
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  6. #6
    Join Date
    Dec 2002
    Location
    Tenby, Wales
    Posts
    277

    Re: Removing Rows from DataGridView

    Hey Hannes

    I haven't tried it that way but I got it working with the following code:
    Code:
    For Each row As DataGridViewRow In DataGridView1.Rows
                    If row.Cells("Merits").Value = 0 And row.Cells("Demerits").Value = 0 Then ' In other words, if nothing has been entered for that student
                        Dim i As Integer = 0
                        While i < DataGridView1.Rows.Count
                            If DataGridView1.Rows(i).Cells("Description").Value = "" And DataGridView1.Rows(i).Cells("Action").Value = "" Then
                                DataGridView1.Rows.RemoveAt(i)
                            Else
                                i += 1
                            End If
                        End While
                    End If
                Next
    Visual Basic 2005 ver. 8.0.50727.867

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Removing Rows from DataGridView

    Notice the RED difference?

    Code:
    For Each row As DataGridViewRow In DataGridView1.Rows
                    If row.Cells("Merits").Value = 0 And row.Cells("Demerits").Value = 0 Then ' In other words, if nothing has been entered for that student
                        Dim i As Integer = row
                        While i < DataGridView1.Rows.Count
                            If DataGridView1.Rows(i).Cells("Description").Value = "" And DataGridView1.Rows(i).Cells("Action").Value = "" Then
                                DataGridView1.Rows.RemoveAt(i)
                            End If
                        End While
                    End If
                Next
    (unless records are intermixed)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)