Results 1 to 6 of 6

Thread: [RESOLVED] Am I clearing my DataGridView and unbinding my textboxes correctly?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2022
    Location
    Manitoba, Canada
    Posts
    142

    Resolved [RESOLVED] Am I clearing my DataGridView and unbinding my textboxes correctly?

    Working with VB.NET and SQLite:

    Let's say that I populate a DataGridView using a BindingSource like in the example below:
    Code:
            dtMaster = New DataTable
            mtAdpater.Fill(dtMaster)
            mtBindingSource.DataSource = dtMaster
            Me.DGV.DataSource = mtBindingSource
    Then let's say that I also have objects on the form that are bound to the same BindingSource like in the example below:
    Code:
            Me.txtOne.DataBindings.Add("Text", mtBindingSource, "One")
            Me.txtTwo.DataBindings.Add("Text", mtBindingSource, "Two")
            Me.txtThree.DataBindings.Add("Text", mtBindingSource, "Three")
    Now, I want to clear the DataGridView and unbind those form objects with a button click event. Is this the correct method of doing so?:
    Code:
            Me.DGV.DataSource = vbNull
    
            Me.txtOne.DataBindings.Clear()
            Me.txtTwo.DataBindings.Clear()
            Me.txtThree.DataBindings.Clear()
    
            Me.txtOneName.Text = ""
            Me.txtTwoName.Text = ""
            Me.txtThree.Text = ""
    This appears to clear the DataGridView while unbinding and clearing the textboxes on the form like I want to and I'm not getting any errors that I can see. I just want to make certain that I'm doing this correctly.

    After I clear everything I can then repopulate the DataGridView and rebind the objects just fine. I have noticed however, that the last row that I had highlighted in the DataGridView prior to clearing it, will be the same row that is highlighted once I repopulate it. This strikes me as a little odd?

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    Re: Am I clearing my DataGridView and unbinding my textboxes correctly?

    I could be wrong about this, but I don't think you should be clearing the datasource or unbind the textboxes. Just refresh the binding source ... that's the point of it ...
    The actions taken against the binding source allow it to be automatically reflected in any bound control.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Am I clearing my DataGridView and unbinding my textboxes correctly?

    Why do you want to Clear and then reset the data bindings?

    If this has to do with repopulating the DGV after a search then all you need to do is, someBindingsource.Filter = Nothing.

    Also, this question has nothing to do with Database programming and should be posted in the .Net programming forum.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: Am I clearing my DataGridView and unbinding my textboxes correctly?

    There's no reason for you to be unbinding anything. If you do need to touch the database again - it's not actually clear that you even need to do that - then all you need to do is change the contents of the DataTable. You can Clear the Rows collection and then Fill it again. All the same bindings should and would stay in place. You would probably call SuspendBinding on the BindingSource first and then ResumeBinding when you're done, so that the UI only sees a single change.

    If necessary, you can set the Current property of the BindingSource to the first record after repopulating. Setting Position to 0 would probably be the easier.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2022
    Location
    Manitoba, Canada
    Posts
    142

    Re: Am I clearing my DataGridView and unbinding my textboxes correctly?

    Thank you everyone. I have much to learn - I'm doing my best.

    I'm sorry that I put this in the wrong forum. I thought that perhaps since I was binding data from a database to objects on the form that this may be the place to ask. I see now that I was wrong.

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

    Re: Am I clearing my DataGridView and unbinding my textboxes correctly?

    Quote Originally Posted by The_Hobbyist View Post
    I'm sorry that I put this in the wrong forum. I thought that perhaps since I was binding data from a database to objects on the form that this may be the place to ask. I see now that I was wrong.
    Moving the data between the database and the DataTable would be a database question. Given that DataTable can be used without any database being involved, it's not really about databases. In fact, this question is more about the UI than anything else, as data binding is specifically a Ui issue.

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