.NET 2.0+ WinForms Data-binding
C# version here.
There seems to be a lot of misconception, misinformation and downright lack of knowledge where data-binding is concerned. I'm a big fan of data-binding. I use it at every opportunity and I suggest that everyone else do the same. To that end, I decided that it would be a good idea to start a thread dedicated to the topic.
I'll add posts over time dedicated to different aspects of data-binding and hopefully I can help dispel some of the misconceptions and increase the knowledge of those who would like to save themselves some time writing laborious code to shuffle data back and forth between business objects or other lists and their UI.
Before I begin, I should point out that data-binding is not a panacea. It is just one of the tools in your tool box. Use it when it will help you accomplish your goal, especially when it will save you time. That said, it won't always be the right tool for the job. A good developer will be able to recognise those occasions and write their own code to do the job.
Now, one of the things I hear people say is that data-binding doesn't offer the control that writing your own code. To those people I say that that's a crock. Data-binding offers tremendous power out of the box, plus you can customise many aspects of it to accomplish many different things. If a situation comes along that is too curly for data-binding you still have the freedom to write your own custom routines. That doesn't mean you shouldn't use data-binding for the meat and potatoes though.
The first genuine instalment will be arriving very soon.
Re: .NET 2.0+ WinForms Data-binding
Hi jmcilhinney,
Thanks for your time creating these submissions to the CodeBank.
Turns out DataBindings are exactly what I need at various points in my app.
So I'm trying to get my head wrapped around DataBindings.
I'm going with testing one case first to make sure I understand these things.
Background: I have a TreeView with a Clear button beside the TreeView. The Clear button removes every node from the tree. If there are no nodes, then the Clear button should be disabled. This is what I want, but I'm certain that the syntax is incorrect.
vb.net Code:
btnSequenceClear.DataBindings.Add("Enabled", tvwSequence.Nodes.Count, "0", False, DataSourceUpdateMode.OnPropertyChanged)
What am I doing wrong?
Re: .NET 2.0+ WinForms Data-binding
I ran my app with the DataBinding and i get this error:
Cannot bind to the property or column 0 on the DataSource. Parameter name: dataMember.
Re: .NET 2.0+ WinForms Data-binding
Quote:
Originally Posted by
JugglingReferee
Hi jmcilhinney,
Thanks for your time creating these submissions to the CodeBank.
Turns out DataBindings are exactly what I need at various points in my app.
So I'm trying to get my head wrapped around DataBindings.
I'm going with testing one case first to make sure I understand these things.
Background: I have a TreeView with a Clear button beside the TreeView. The Clear button removes every node from the tree. If there are no nodes, then the Clear button should be disabled. This is what I want, but I'm certain that the syntax is incorrect.
vb.net Code:
btnSequenceClear.DataBindings.Add("Enabled", tvwSequence.Nodes.Count, "0", False, DataSourceUpdateMode.OnPropertyChanged)
What am I doing wrong?
The second parameter is the object you want to bind to and the third parameter is the property of that object you want to bind to. The first parameter is the property of the control you want to bind to. The type of those two properties must match if you're to pass a value back and forth between them.
First up, tvwSequence.Nodes.Count is an Integer. Does the Integer type have a property named "0"? No it doesn't, hence the error message.
What you need to do is bind the Enabled property of your Button, which is type Boolean, to some other Boolean property of some other object that returns True when the tree contains nodes and False when it doesn't. You currently have no such property, so you either need to create one or else you can't use data-binding.
Re: .NET 2.0+ WinForms Data-binding
Quote:
Originally Posted by
jmcilhinney
The second parameter is the object you want to bind to and the third parameter is the property of that object you want to bind to. The first parameter is the property of the control you want to bind to. The type of those two properties must match if you're to pass a value back and forth between them.
First up, tvwSequence.Nodes.Count is an Integer. Does the Integer type have a property named "0"? No it doesn't, hence the error message.
What you need to do is bind the Enabled property of your Button, which is type Boolean, to some other Boolean property of some other object that returns True when the tree contains nodes and False when it doesn't. You currently have no such property, so you either need to create one or else you can't use data-binding.
Great answer. Thanks. It all makes much more sense now.
I wonder if I can use the Tag property somehow.
Re: .NET 2.0+ WinForms Data-binding
jmcilhinney,
Hi! I'm trying to databind a pair of textboxes to a dataset based on a stored procedure and for some reason it's failing. Where am I going wrong?
Edit: Form1.adid is a variable passed from another form and captured from the system environment of the user that correllates with the parameter of the stored procedure. Just FYI so you know why it's there.
Code:
connection.Open()
'populate text boxes
Dim cmSQL As SqlCommand = New SqlCommand("getAgent", connection)
cmSQL.CommandType = CommandType.StoredProcedure
cmSQL.Parameters.AddWithValue("@IID", Form1.adid)
Dim daAgent As New SqlDataAdapter(cmSQL)
daAgent.SelectCommand = cmSQL
Dim dsAgent As New DataSet
daAgent.Fill(dsAgent)
Try
Me.TextBox1.DataBindings.Add("Text", dsAgent.Tables(0).Columns(0), "Name")
Me.TextBox2.DataBindings.Add("Text", dsAgent.Tables(0).Columns(1), "[Sprint Email]")
Catch ex As System.Exception
'System.Windows.Forms.MessageBox.Show(ex.Message)
MsgBox("Unable to retrieve your Name and Email address.", MsgBoxStyle.Exclamation, "Database Error")
End Try
Many thanks in advance!
Re: .NET 2.0+ WinForms Data-binding
Quote:
Originally Posted by
ZoeWashburn
jmcilhinney,
Hi! I'm trying to databind a pair of textboxes to a dataset based on a stored procedure and for some reason it's failing. Where am I going wrong?
Edit: Form1.adid is a variable passed from another form and captured from the system environment of the user that correllates with the parameter of the stored procedure. Just FYI so you know why it's there.
For a start, you're ignoring the actual exception and telling the user that the data couldn't be retrieved when, in actual fact, it's the binding that's failing. The exception is provided to you for a reason. Use the information it contains to diagnose the issue. If you'd like us to diagnose the issue for you, give us the information contained in the exception.
Re: .NET 2.0+ WinForms Data-binding
Thanks! Turns out what I really needed was a fresh pair of eyes on the code. I got it fixed this morning. Needed to adjust the syntax on the databindings. :)