# Binding data to a ComboBox and then showing ValueMember in another TextBox

I have a comboBox and a TextBox in the View and when the view is loaded, the ComboBox should be populated. After that when a user selects particular item from the ComboBox, its related ValueMember should be displayed in the TextBox.

The View doesn't know about DAL or Presenter and, it only has properties and events.

VIEW

public partial class frmBankAccount : Form, IBankAccountView
{

{
set { cmbName.DataSource = value; cmbName.DisplayMember = "Name"; cmbName.ValueMember = "Emp_ID"; }
}

private void frmBankAccount_Load(object sender, EventArgs e)
{
}

private void cmbName_SelectedValueChanged(object sender, EventArgs e)
{
txtEID.Text = cmbName.SelectedValue.ToString();
}

}


When the event fires, Presenter gets a DataTable from DataService and binds it to the ComboBox through the property.

PRESENTER

class BankAccountPresenter : BasePresenter
{
public BankAccountPresenter(IBankAccount model, IBankAccountView view, IDataService dataService )
{
_DataService = dataService;
_Model = model;
_View = view;

this.WireUpViewEvents();
}

private void WireUpViewEvents()
{
}

private void _View_OnLoadEmployees(object sender, EventArgs e)
{
}

{
BindingSource bs = new BindingSource();
bs.DataSource = _DataService.LoadEmplyees(); //DataService returns a DataTabel with "Emp_ID" and "Name"
}

//Main window will call this method to load the View
public void Show()
{
_View.ShowDialog();
}

}


NOTE: When populating ComboBox since I need only two fields (Emp_ID and Name), The DataService returns a DataTable instead of the Model as the model has about 10 other fields as well. The Model will be used in subsequent tasks like Search, Insert, Update etc.

Could you please review this code and provide your feedback? You may give your attention to the way that I've used properties to set DisplayMember and ValueMember of the Combo. If there is anything which is not acceptable kindly let me know with an alternate solution.

• Unless you are supporting some 10 years old project, you should consider switching to wpf. Winforms is an obsolete framework. – Nikita B Jun 30 '14 at 10:56

The only nitpick I have is the bs variable. I'm not a fan of ultra short names of any kind. You could call it source or some such thing. Seriously though, that's a huge nitpick. I just couldn't find anything else to comment on.