Hi im new to C# and I have a form with customer data on it. There is a combo box with the customer name and some address fields. When I select the customer name I would like the address field to be automatically updated from the customer table. Can anyone help me with the code for this. Thanks in advance.

Dani AI

Generated

As described, the goal is to have the address fields update when a customer is picked. is right that binding is the easiest, and ’s manual lookup is a valid fallback. Two practical patterns follow: one uses WinForms data binding so the textboxes update automatically, the other shows a simple, efficient manual lookup (without repeating the exact code already posted).

Binding approach (recommended for forms that edit/display a single "current" record):

var bs = new BindingSource();
bs.DataSource = customersTable;             // DataTable or List<T>
comboBox.DataSource = bs;
comboBox.DisplayMember = "CustomerName";
comboBox.ValueMember = "CustomerID";

textBoxStreet.DataBindings.Add("Text", bs, "Street", true, DataSourceUpdateMode.OnPropertyChanged);
textBoxCity.DataBindings.Add("Text", bs, "City");

Because the combo and textboxes share the same BindingSource, changing the combo position updates the textboxes automatically. Use a unique ID for ValueMember so duplicate names don’t cause ambiguity. Guard for DBNull when necessary.

Manual lookup (useful if you want to load details on demand or do a DB call per selection):

customersTable.PrimaryKey = new[] { customersTable.Columns["CustomerID"] };
int id = (int)comboBox.SelectedValue;
DataRow row = customersTable.Rows.Find(id);
if (row != null) {
    textBoxStreet.Text = row.Field<string>("Street") ?? "";
    textBoxCity.Text = row.Field<string>("City") ?? "";
}

Or read straight from the selected item:

var drv = (DataRowView)comboBox.SelectedItem;
textBoxStreet.Text = drv["Street"].ToString();

Practical tips: if the list is large, bind only names/IDs and fetch addresses asynchronously on selection to avoid UI hangs. Prevent SelectedIndexChanged from running during Form_Load by temporarily removing the handler or using an “isInitializing” flag. Always check for null/DBNull and prefer IDs for lookups to avoid wrong matches when names repeat.

Recommended Answers

All 3 Replies

thats normally why you bind the control to the table/query

thats normally why you bind the control to the table/query

This form is used to fill out another table for quoting. The customer table is used as reference to fill out the customer portion of the quote form.

As LizR said, binding is the best approach, however you can manually locate the Customer in the table and populate the address text box.

string custName = combobox.Text;
DataRow[] addressInfo = custData.Select(string.Format("Customer='{0}'",custName));
if(addressInfo.Length > 0)
{
    edStreet.Text = (string)addressInfo[0]["Street"];
    edCity.Text = (string)addressInfo[0]["City"];
    // and so on...
}

Hope this helps,
Jerry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.