Simple form with databinding

dsk96m

Well-known member
Joined
Jan 11, 2013
Messages
173
Programming Experience
1-3
I know this will be easy for you guys. I just cant figure it out. I have been away from it for awhile and forgot a lot.

I have a form with a combobox (it is actually a devexpress lookupedit) that i have my employee table from my dataset as the datasource. In that i have the employees name. In the same table there is a lot of other information. What I would like to do is have a text boxes for that other information (empl_id, location, etc). So if i set the databinding on those text boxes to the appropriate bindingsource and field, it works.

But what I need to do is when the form is opened those fields to be empty until the user selects an employee from the combobox. No matter what I do, those other fields always come up with data in them when the form loads. I cant wait to fill the tableadapter because it is used for the drop down.

Any help would be appreciated. I am using vb.net

Thank you
 
You will need to wait until the user makes a selection in the ComboBox before binding the TextBox(es). When you bind, the first record is selected by default. With a ComboBox, you can set the SelectedItem or SelectedIndex to remove that selection but that's not possible with a TextBox. As such, if you don't want an item selected then you can't bind. Here's an example of binding a TextBox on the first user selection from a ComboBox.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim table As New DataTable

    With table.Columns
        .Add("ID", GetType(Integer))
        .Add("Name", GetType(String))
        .Add("Description", GetType(String))
    End With

    With table.Rows
        .Add(1, "Peter", "First")
        .Add(2, "Paul", "Second")
        .Add(3, "Mary", "Third")
    End With

    Me.BindingSource1.DataSource = table

    With Me.ComboBox1
        .DisplayMember = "Name"
        .ValueMember = "Description"
        .DataSource = Me.BindingSource1
    End With

    Me.ComboBox1.SelectedItem = Nothing
End Sub

Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
    If Me.TextBox1.DataBindings("Text") Is Nothing Then
        Me.TextBox1.DataBindings.Add("Text", Me.BindingSource1, "Description")
    End If
End Sub
You can add as many TextBoxes as you want in the SelectionChangeCommitted event handler. Note that you could add multiple data-bindings inside the If block but you would still only ever need to test one, because either they have all been added or none of them have.
 
Last edited:
I have to clear the data bindings on each new selection then, correct.



You will need to wait until the user makes a selection in the ComboBox before binding the TextBox(es). When you bind, the first record is selected by default. With a ComboBox, you can set the SelectedItem or SelectedIndex to remove that selection but that's not possible with a TextBox. As such, if you don't want an item selected then you can't bind. Here's an example of binding a TextBox on the first user selection from a ComboBox.[highlight=vb.net]Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim table As New DataTable

With table.Columns
.Add("ID", GetType(Integer))
.Add("Name", GetType(String))
.Add("Description", GetType(String))
End With

With table.Rows
.Add(1, "Peter", "First")
.Add(2, "Paul", "Second")
.Add(3, "Mary", "Third")
End With

Me.BindingSource1.DataSource = table

With Me.ComboBox1
.DisplayMember = "Name"
.ValueMember = "Description"
.DataSource = Me.BindingSource1
End With

Me.ComboBox1.SelectedItem = Nothing
End Sub

Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
If Me.TextBox1.DataBindings("Text") Is Nothing Then
Me.TextBox1.DataBindings.Add("Text", Me.BindingSource1, "Description")
End If
End Sub
[/highlight]You can add as many TextBoxes as you want in the SelectionChangeCommitted event handler. Note that you could add multiple data-bindings inside the If block but you would still only ever need to test one, because either they have all been added or none of them have.
 
I have to clear the data bindings on each new selection then, correct.
No, you have to do as I've demonstrated. Do I clear any data-bindings anywhere? That code tests whether there is a data-binding already and, if not, it adds one. That means that it's going to test every time the user makes a selection but only on the first occasion will there be no existing data-binding because a data-binding is added on that first occasion.

By the way, sorry about the code formatting. I use multiple forums that use different tags for formatting and I used the wrong ones. I was trying to fix it but for some reason I was unable to edit the post from my laptop on the train. I'm now at work and have successfully fixed the code formatting from my desktop.
 
No problem, i see what you mean, didnt see it as i was viewing on a blackberry. I see how you are doing it. Thank you. Another quick question related.

I have my parent table, employee and a lookup table jobcd. in the employee table i have employe_jobcd_id which should reference the jobcd_id in the jobcd table. So it the text box in the above for employee_jobcd_id, i want to display the jobcd_desc from the jobcd table instead of the employee_jobcd_id. See picture.

Untitled.png

So in our discussion above we would have had:
me.jobtitle.databindings.add("text",temsemplbindingsource,"empl_jobcd_id")

How do I instead display the jobcd_desc from the lookup table?
 
Last edited:
Back
Top