-
Jan 11th, 2023, 09:51 AM
#1
Thread Starter
Addicted Member
[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?
-
Jan 11th, 2023, 10:06 AM
#2
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
-
Jan 11th, 2023, 03:01 PM
#3
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.
-
Jan 11th, 2023, 11:26 PM
#4
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.
-
Jan 12th, 2023, 01:31 AM
#5
Thread Starter
Addicted Member
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.
-
Jan 12th, 2023, 01:44 AM
#6
Re: Am I clearing my DataGridView and unbinding my textboxes correctly?
Originally Posted by The_Hobbyist
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|