# Visual Basic > Database Development >  [RESOLVED] Am I clearing my DataGridView and unbinding my textboxes correctly?

## The_Hobbyist

Working with VB.NET and SQLite:

Let's say that I populate a DataGridView using a BindingSource like in the example below:


```
        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:


```
        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?:


```
        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?

----------


## techgnome

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

----------


## wes4dbt

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.

----------


## jmcilhinney

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.

----------


## The_Hobbyist

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.

----------


## jmcilhinney

> 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.

----------

