Databound label text property

Pace

Well-known member
Joined
Sep 2, 2005
Messages
90
Location
Manchester UK! :)
Programming Experience
1-3
Databound label text property (RESOLVED)

[FONT=Verdana, Geneva, Arial, Sans-serif]Hi, [/FONT]
[FONT=Verdana, Geneva, Arial, Sans-serif]I have a form with a combo box... I have a label that is bound to a column that displays info based on whats selected in the combo. [/FONT]
[FONT=Verdana, Geneva, Arial, Sans-serif]It looks fine when I first load the form. When I change the index of the CBO the text of the label stays the same. I know I need to write code to do this in the SelectedIndexChanged event of the CBO but im struggling. [/FONT]
[FONT=Verdana, Geneva, Arial, Sans-serif]Can someone tell me how once the index is changed in the CBO, I can get the label's text property to change appropriately to that of the value it should be?[/FONT]

[FONT=Verdana, Geneva, Arial, Sans-serif]Thanks![/FONT]
[FONT=Verdana, Geneva, Arial, Sans-serif]Pace[/FONT]
 
Last edited:
You don't need to write any code. Add a BindingSource to your form and then bind that to both the ComboBox and the Label. Bind your DataTable to the BindingSource and everything will just work. Here's a quick demo with everything done in code. Add a Label, ComboBox and BindingSource to a form, then add this code:
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim dt As New DataTable

    dt.Columns.Add("ID", GetType(Integer))
    dt.Columns.Add("Description", GetType(String))

    Dim dr As DataRow

    For i As Integer = 1 To 10
        dr = dt.NewRow
        dr("ID") = i
        dr("Description") = "Item " & i
        dt.Rows.Add(dr)
    Next

    Me.BindingSource1.DataSource = dt

    Me.ComboBox1.DisplayMember = "Description"
    Me.ComboBox1.DataSource = Me.BindingSource1

    Me.Label1.DataBindings.Add("Text", Me.BindingSource1, "ID")
End Sub
Change the selection in the ComboBox and watch the Label change with it. Isn't data-binding the shizzle? ;)
 
Hi, is this solution based on the .Net 1.1 platform?
 
Your profile says that your primary platform is 2005 and you didn't say any different in your post, so there's no reason that it should be. Are you one of those folks who was prompted to enter some required fields and didn't bother checking whether the defaults were correct? If so then I suggest that you change it now. It is fortunate that the 1.x solution is almost the same as the 2.0 or I would have wasted my time, and that's a bad thing. Just leave out the BindingSource and bind the controls to the DataTable directly.
 
jmc im sorry if I have offended you.

I tired this as soon as I got in the office before my coffee and I wasnt seeing the appropriate results... now with coffe having another go I can see that im starting to make progress... Once again I apologise and thank you for your time.

Pace
 
So... you are using 2.0 and you didn't think it was working and you thought that it be because I'd provided code for 1.1? Now I get it. Not offended. I thought that I was annoyed but apparently I wasn't. :) Mind you, you must reeeeeally need that coffee in the morning. ;)
 
Thanks again JMC!

Im still not getting anywhere... im not sure if its to do with my dataset?

Here is some code of whats happening...

PrivateSub formDirectReceipt_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
Me.ProductTableAdapter.Fill(DsStockReceipt.Product, cellVar)
EndSub

PrivateSub cboReceiptProduct_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboReceiptProduct.SelectedIndexChanged
'add event here... see http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=636545&SiteID=1&mode=1
Me.TextBox1.DataBindings.Clear()
Me.TextBox1.DataBindings.Add("Text", Me.ProductBindingSource1, "Short Description")
EndSub
 
Just a thought, but if your combo and label are bound to the table then the label should automatically changes it's value. Is this not happening?
 
thats correcct vis.

I cant get it to change... its poor though as I have a lot of stored procedures and data changes on the go, now having this problem I feel so stupid that I cant get something as seemingly simple as this to work.
 
Why are you clearing the TextBox's DataBindings in the ComboBox's SelectedIndexChanged event? Where in my code is there any indication that that is a good idea. In fact, I specifically said that you don't have to do anything on the SelectedIndexChanged.
 
I know... its just im really struggling with this, I dont want to add my own colums using .add with a description, im using a DataAdapter to pull that information from a database... I'm not convinced I understand what you are saying properly so im doing my best to interperit it.

My best guess at taking the solution you kindly provided me with coupled with my problem looks like;

Dim dt As New DataTable
dt.Columns.Add(
Me.DsStockReceipt.Product.Product_CodeColumn)
dt.Columns.Add(
Me.DsStockReceipt.Product.Short_DescriptionColumn)
Dim dr As DataRow
For i As Integer = 1 To 2
dr = dt.NewRow
dr(
"Code") = i
dr(
"Description") = "Item " & i
dt.Rows.Add(dr)
Next
Me.BindingSource1.DataSource = dt
Me.ComboBox1.DisplayMember = "Code"
Me.ComboBox1.DataSource = Me.BindingSource1
Me.Label1.DataBindings.Add("Text", Me.BindingSource1, "Code")
End Sub

This gives me; "Column 'Product Code' already belongs to another DataTable." though I created this dataset specific for this form. Think I need a new book...
 
You don't have to create your own columns in code. The point is that you need data in a DataTable. How that data gets there is immaterial. This is the bit that does the binding:
VB.NET:
Me.BindingSource1.DataSource = dt

Me.ComboBox1.DisplayMember = "Description"
Me.ComboBox1.DataSource = Me.BindingSource1

Me.Label1.DataBindings.Add("Text", Me.BindingSource1, "ID")
How you create an populate the DataTable is completely up to you becaus eit has no bearing whatsoever on the binding. If you have a typed DataSet, or an untyped dataSet but you've specified the table schema at design time, then you don't need ANY code at all. All of the binding can be done in the designer. My code is for example only. It's the PRINCIPLE I'm trying to get across. Put data in DataTable, bind DataTable to BindingSource, bind BindingSource to controls. That's what I said and that's what I did.
jmcilhinney said:
Add a BindingSource to your form and then bind that to both the ComboBox and the Label. Bind your DataTable to the BindingSource and everything will just work.
I didn't say that your code had to be exactly the same.
 
I just dont seem to get anywhere with this one...

If by any chance someone still reads this, here is the code im using;

Private Sub formDirectReceipt_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strCnn, strSQL As String
strCnn = "provider=sqloledb;data source=pe750-d;initial catalog=DummyHSS;trusted_connection=yes;"
strSQL = "SELECT [Product Code], [Short Description], Cost, ProductionGroup " & _
"FROM Product " '& _
'"WHERE Product.[Product Code] = @prdCode AND Product.ProductionGroup = @cellVar"
Dim da As New OleDb.OleDbDataAdapter(strSQL, strCnn)
Dim dt As New DataTable
Dim dsStockReceipt As New DataSet
da.Fill(dsStockReceipt,
"Product")
dt.Columns.Add(
"Code", GetType(String))
dt.Columns.Add(
"Description", GetType(String))
Dim dr As DataRow
For i As Integer = 1 To 2
dr = dt.NewRow
dr(
"Code") = dt.Columns("Code")
dr(
"Description") = dt.Columns("Description")
dt.Rows.Add(dr)
Next
Me.BindingSource1.DataSource = dt
Me.cboReceiptProduct.DisplayMember = "Description"
Me.cboReceiptProduct.DataSource = Me.BindingSource1
Me.TextBox1.DataBindings.Add("Text", Me.BindingSource1, "Description")
End Sub

I cant get any data in or out of it, in fact I cant do jack and im just too frustrated to work out why... ive tried till im blue in the face, basically im just sick of looking at the same sub so I have to move on before I get fired.
 
Ok, how about this...

DataTable -> dt
Dataset -> dsStockReceipt

You fill the dataset with the dataadpater which creates a new table inside the dataset.

This is the strange bit....

You then add a couple of new rows to the datatable and try to fill it with info from dt,
and then bind your control to it... err... there's nothing in it my friend.

Try adding the datatable to the dataset. then bind like this...

VB.NET:
Me.BindingSource1.DataSource = dsstockreciept.tables(0)
Me.cboReceiptProduct.DisplayMember = "Description"
Me.cboReceiptProduct.DataSource = Me.BindingSource1
Me.TextBox1.DataBindings.Add("Text", Me.BindingSource1, "Description")

Or am i just going nuts and can't see the wood for the trees?
 
You see, this is why I don't like posting code. People cut and paste your code without understanding what it does. You've included all my code in yours when it's not needed. You're filling one DataTable from your database, then you're not even binding that DataTable to your controls. You have a DataSet and you're Filling a DataTable named "Product" in that DataSet from your database. That's cool. Then you go and create a completely different DataTable just because I did in my example and bind that to your controls. You already have your DataTable so bind it. Do NOT create another one.
VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] formDirectReceipt_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
     [/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strCnn, strSQL [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] String

     [/COLOR][/SIZE][SIZE=2]strCnn = [/SIZE][SIZE=2][COLOR=#800000]"provider=sqloledb;data source=pe750-d;initial catalog=DummyHSS;trusted_connection=yes;"
     [/COLOR][/SIZE][SIZE=2]strSQL = [/SIZE][SIZE=2][COLOR=#800000]"SELECT [Product Code], [Short Description], Cost, ProductionGroup "[/COLOR][/SIZE][SIZE=2] & _
[/SIZE][SIZE=2][COLOR=#800000]"FROM Product "[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'& _
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'"WHERE Product.[Product Code] = @prdCode AND Product.ProductionGroup = @cellVar"

    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] da [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] New[/COLOR][/SIZE][SIZE=2] OleDb.OleDbDataAdapter(strSQL, strCnn)[/SIZE][SIZE=2][COLOR=#0000ff]
    Dim[/COLOR][/SIZE][SIZE=2] dsStockReceipt [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] New[/COLOR][/SIZE][SIZE=2] DataSet

    da.Fill(dsStockReceipt, [/SIZE][SIZE=2][COLOR=#800000]"Product"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]    Me[/COLOR][/SIZE][SIZE=2].BindingSource1.DataSource = dsStockReceipt.Tables("Product")
    [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].cboReceiptProduct.DisplayMember = [/SIZE][SIZE=2][COLOR=#800000]"Description"
    [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].cboReceiptProduct.DataSource = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].BindingSource1
    [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TextBox1.DataBindings.Add([/SIZE][SIZE=2][COLOR=#800000]"Text"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].BindingSource1, [/SIZE][SIZE=2][COLOR=#800000]"Description"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff] Sub[/COLOR][/SIZE]
Just like I said before: populate a DataTable, bind the DataTable to the BindingSource, bind the BindingSource to the controls.
 
Back
Top