Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Simple question on ComboBox Databinding look up.
 

Simple question on ComboBox Databinding look up.

I have the following and simple scenario.

  • 01 Combo -2 textBoxes
  • Databind a comboBox to a list of Employees
  • when user select an employee in the combo
  • set the txtName and txtSurname to map to the employee selected.

How can I do that using databinding?

I have to confess I have hardly used databinding and always done programmatically.

programmatically I would do something like

Code Snippet

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

LoadComboBox();

}

private void LoadComboBox()

{

this.cboEmployee.BeginUpdate();

this.cboEmployee.Items.Clear();

List<Employee> empList = GetEmployeeList();

foreach (Employee item in empList)

{

cboEmployee.Items.Add(item);

}

this.cboEmployee.SelectedIndex = 0;

this.cboEmployee.EndUpdate();

}

private List<Employee> GetEmployeeList()

{

List<Employee> employeeList = new List<Employee>();

employeeList.Add(new Employee("Marco", "Blogg"));

employeeList.Add(new Employee("Jo", "Smith"));

employeeList.Add(new Employee("Diego", "Maradona"));

return employeeList;

}

private void cboEmployee_SelectedIndexChanged(object sender, EventArgs e)

{

if (this.cboEmployee.SelectedItem != null)

{

Employee item = (Employee)cboEmployee.SelectedItem;

this.txtName.Text = item.Name ;

this.txtSurname.Text = item.Surname;

}

}

}

public class Employee

{

private string name=string.Empty;

private string surname = string.Empty;

public Employee(string name,string surname)

{

this.name = name;

this.surname = surname;

}

public string Name

{

get { return this.name; }

set { this.name = value; }

}

public string Surname

{

get { return this.surname; }

set { this.surname = value; }

}

public override string ToString()

{

return string.Format("{0} {1}", this.surname, this.name);

}

}

What is the equivalent in databinding.
devBrix  Wednesday, September 03, 2008 2:15 PM
If you bind the combobox and the textboxes to the same list they will stay on the same record when you change the selection in the combobox

private void Form1_Load(object sender, EventArgs e)
{
List<Employee> el = GetEmployeeList();
cboEmployee.DataSource = el;
txtName.DataBindings.Add("Text", el, "Name");
txtSurName.DataBindings.Add("Text", el, "Surname");
}

Ken Tucker  Wednesday, September 03, 2008 5:40 PM
If you bind the combobox and the textboxes to the same list they will stay on the same record when you change the selection in the combobox

private void Form1_Load(object sender, EventArgs e)
{
List<Employee> el = GetEmployeeList();
cboEmployee.DataSource = el;
txtName.DataBindings.Add("Text", el, "Name");
txtSurName.DataBindings.Add("Text", el, "Surname");
}

Ken Tucker  Wednesday, September 03, 2008 5:40 PM

Fantastic.That easy!!

thanks again for your time

devBrix  Wednesday, September 03, 2008 8:42 PM

You can use google to search for other answers

Custom Search

More Threads

• I am having update trouble
• UpdateCommand, query and databinding...
• DataBinding source not working properly
• how to get difference between two dates in number of day,month,year format
• DataGridView RowLeave
• Using BindingNavigator with multiple BindingSources
• Please help Me !
• loop unbound datagrid column
• check if data in a dataset's column alrady exists
• Icons in a datagrid