Results 1 to 9 of 9

Thread: [RESOLVED] Remove DGV ros via Quary

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2013
    Location
    Newcastle, Australia
    Posts
    158

    Resolved [RESOLVED] Remove DGV ros via Quary

    I am using a quary to search and show results in a databound DGV on the fly
    Like so
    (THIS WORKS FINE)
    Code:
                Try
                    Me.FilesTableAdapter.NewProfile(Me.FilesDataSet.Files, NameTextbox.Text)
                Catch ex As System.Exception
                    System.Windows.Forms.MessageBox.Show(ex.Message)
                End Try
    i need to know how to delete all the found results

    i.e

    If someone clicks a Delete Button on the form then it runs this quary and deletes all found results

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Remove DGV ros via Quary

    In your code, 'Me.FilesDataSet.Files' is the DataTable that contains all the records. One option is to loop through that DataTable and call the Delete method of each row, then call Update on the table adapter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2013
    Location
    Newcastle, Australia
    Posts
    158

    Re: Remove DGV ros via Quary

    Would you be able to do a brief example of that sorry i get what you are saying just dont know how i would do it mainly the loop part

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2013
    Location
    Newcastle, Australia
    Posts
    158

    Re: Remove DGV ros via Quary

    i just tried

    Code:
                    For Each row As DataRow In FilesDataGridView.Rows
    
                        ' Try
                        '    Me.FilesTableAdapter.NewProfile(Me.FilesDataSet.Files, Prifle_NameTextBox.Text)
                        ' Catch ex As System.Exception
                        '    System.Windows.Forms.MessageBox.Show(ex.Message)
                        'End Try
                        Me.FilesBindingSource.RemoveCurrent()
    
                    Next
    But i keep getting an error
    Invalid cast exception was unhandled
    unable to cast object of type 'system.windows.forms.datagridviewrow' to type 'system.datarow'

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2013
    Location
    Newcastle, Australia
    Posts
    158

    Re: Remove DGV ros via Quary

    this works but it doesnt loop all the way it deletes a few and not all rows on the datgridview

    Code:
                    For Each row As DataGridViewRow In FilesDataGridView.Rows
    
                        Me.FilesBindingSource.RemoveCurrent()
    
                    Next
    
                    Me.FilesBindingSource.EndEdit()
                    Me.TableAdapterManager.UpdateAll(Me.FilesDataSet)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Remove DGV ros via Quary

    You're looping through the DataGridView and calling RemoveCurrent on the BindingSource. I said:
    loop through that DataTable and call the Delete method of each row
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2013
    Location
    Newcastle, Australia
    Posts
    158

    Re: Remove DGV ros via Quary

    how do i do that?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Remove DGV ros via Quary

    If you can loop through one collection then you can loop through any collection. A typed DataTable, which you're using, is a collection just like the Rows of a DataGridView. You don't have to be taught how to drive every different car. Drive one, drive 'em all. Same goes for looping through collection. As for calling the Delete method, it's calling a method. Call one method, call 'em all. Programming is about taking what you know and applying it to similar but not identical situations. Try it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2013
    Location
    Newcastle, Australia
    Posts
    158

    Re: Remove DGV ros via Quary

    got it with
    Code:
                    For Each AttachedFile As DataRow In FilesDataSet.Tables(0).Rows
    
                        If (AttachedFile("File_Profile") = Prifle_NameTextBox.Text) Then
    
                            AttachedFile.Delete()
    
                        End If
    
                    Next
    
                    FilesDataSet.Tables(0).AcceptChanges()
                    Me.ProfilesBindingSource.RemoveCurrent()
    well it seems to work any way

Posting Permissions

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



Click Here to Expand Forum to Full Width