Please enlighten me on the following. I have the simplest form on this earth created with Visual Studio 2008. Within the form constructor after InitializeComponent I create a data set and bind a text box to a column. After calling textBox1.DataBindings.Add("Text", dataSet, "Table1.ID") I can askif the currencyManagerof thetableexitstlike this textBox1.BindingContext.Contains(dataSet, "Table1"). Not surprisingly it returns true because I have the textBox bound to "Table1".
Now try the following. Just before binding the textBox1 control add a line of code that reads the bindingContext of the textBox like this Dim ctx As BindingContext = textBox1.BindingContext. That line of code causes the rest of the code to break. textBox1.BindingContext.Contains(dataSet, "Table1") now returns false! If you know this, please give a minute to explain this to me.
kkt | | kkt Tuesday, August 25, 2009 1:39 PM | Yep, this is a weird one alright, and I'd say it's a bug. Ling, the reason you can't reproduce it is that you're adding the DataBinding before you do all the other stuff: TextBox1.DataBindings.Add("text", dataset, "dt01.col01", True)Dim ctx As BindingContext = TextBox1.BindingContext If TextBox1.BindingContext.Contains(dataset, "dt01") Then
What kkt is talking about is doing creating the ctx variable BEFORE setting the TextBox DataBindings. I was able to reproduce the bug.
Now, kkt, here's a little tidbit about databinding syntax. There are actually two different syntaxes, resulting in two different BindingContexts... the one you're using results in the bug, the other does not. I have always preferred the other syntax (not the one you used). Here they are:
Yours:
TextBox1.DataBindings.Add("Text", dataset, "Table1.ID")
Mine: TextBox1.DataBindings.Add("Text", dataset.Tables["Table1"], "ID")
Now, that said, the main thing to remember is that you have to be consistent throughout your Form. You cannot mix-and-match these two syntaxes on the same Form. Try the second one; I think you'll find it works more consistently.
~~Bonnie Berent [C# MVP] - Marked As Answer byLing WangMSFT, ModeratorThursday, September 03, 2009 9:15 AM
-
| | BonnieB Monday, August 31, 2009 3:48 PM | Sorry for misunderstanding. Yes, I can reproduce this issue now. This only happens in the constructor.
I have found this issue has been submited on the connect websit:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=485537
Thanks for your feedback.
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. - Marked As Answer byLing WangMSFT, ModeratorThursday, September 03, 2009 9:15 AM
-
| | Ling Wang Thursday, September 03, 2009 9:15 AM |
Please enlighten me on the following. I have the simplest form on this earth created with Visual Studio 2008. Within the form constructor after InitializeComponent I create a data set and bind a text box to a column. After calling textBox1.DataBindings.Add("Text", dataSet, "Table1.ID") I can askif the currencyManagerof thetableexitstlike this textBox1.BindingContext.Contains(dataSet, "Table1"). Not surprisingly it returns true because I have the textBox bound to "Table1".
Now try the following. Just before binding the textBox1 control add a line of code that reads the bindingContext of the textBox like this Dim ctx As BindingContext = textBox1.BindingContext. That line of code causes the rest of the code to break. textBox1.BindingContext.Contains(dataSet, "Table1") now returns false! If you know this, please give a minute to explain this to me.
kkt
Is this such a bad question? It is easily reproducible. I've read the documentation in MSDN and lookep up the .NET source code. I've seen that the Control base class redirects calls to the parent control to get the binding context until you hit a ContainerControl - a form in my case - which then does on-demand creation. Still I cannot explain why this is happening.
kkt | | kkt Thursday, August 27, 2009 8:21 AM | The only BindingContext object that will exist on a Form, prior to you doing any kind of DataBinding at all, will be the Form's BindingContext object. ~~Bonnie Berent [C# MVP] | | BonnieB Sunday, August 30, 2009 9:39 PM | Still the behaviour cannot be justified. Binding has been done and simply reading a property changes the behaviour. If you don't read the BindingContext everything works. There is obviously some kind of lazy creation involved that works strangely kkt | | kkt Monday, August 31, 2009 7:16 AM | Hi,
As Bonnie said, the “BindingContext�is a per-form cache. You can try to check Me.bindingcontext object. I also test on my side. Unfortunately, I am not able to reproduce this issue. I use the following code:
Dim dataset As DataSet = New DataSet()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dataset.Tables.Add("dt01")
dataset.Tables("dt01").Columns.Add("col01")
dataset.Tables("dt01").Columns.Add("col02")
dataset.Tables("dt01").Rows.Add("11", "aa")
dataset.Tables("dt01").Rows.Add("22", "bb")
TextBox1.DataBindings.Add("text", dataset, "dt01.col01", True)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ctx As BindingContext = TextBox1.BindingContext
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TextBox1.BindingContext.Contains(dataset, "dt01") Then
Console.WriteLine("contains")
Else
Console.WriteLine("error")
End If
End Sub
Click button2, click button1, click button2, output only shows “contains�
Environment: Visual studio 2008 sp1
I think I may miss something. Could show us your code or provide a demo for us to test? Upload your demo project to some server and offer us the URL. Skydrive (http://skydrive.live.com/ ) may be a good option. You can sign up 25GB free space only with your Windows Live Passport.
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. | | Ling Wang Monday, August 31, 2009 2:47 PM | Yep, this is a weird one alright, and I'd say it's a bug. Ling, the reason you can't reproduce it is that you're adding the DataBinding before you do all the other stuff: TextBox1.DataBindings.Add("text", dataset, "dt01.col01", True)Dim ctx As BindingContext = TextBox1.BindingContext If TextBox1.BindingContext.Contains(dataset, "dt01") Then
What kkt is talking about is doing creating the ctx variable BEFORE setting the TextBox DataBindings. I was able to reproduce the bug.
Now, kkt, here's a little tidbit about databinding syntax. There are actually two different syntaxes, resulting in two different BindingContexts... the one you're using results in the bug, the other does not. I have always preferred the other syntax (not the one you used). Here they are:
Yours:
TextBox1.DataBindings.Add("Text", dataset, "Table1.ID")
Mine: TextBox1.DataBindings.Add("Text", dataset.Tables["Table1"], "ID")
Now, that said, the main thing to remember is that you have to be consistent throughout your Form. You cannot mix-and-match these two syntaxes on the same Form. Try the second one; I think you'll find it works more consistently.
~~Bonnie Berent [C# MVP] - Marked As Answer byLing WangMSFT, ModeratorThursday, September 03, 2009 9:15 AM
-
| | BonnieB Monday, August 31, 2009 3:48 PM | Sorry for misunderstanding. Yes, I can reproduce this issue now. This only happens in the constructor.
I have found this issue has been submited on the connect websit:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=485537
Thanks for your feedback.
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. - Marked As Answer byLing WangMSFT, ModeratorThursday, September 03, 2009 9:15 AM
-
| | Ling Wang Thursday, September 03, 2009 9:15 AM | FYI: I submitted the issue myself as I am pretty sure its a bug. It has been verified by the UIFx team and they have fixed it in .NET 4.0.
Thank you for your contribution guys
kkt | | kkt Thursday, September 03, 2009 11:09 AM |
|