[dba-VB] C# combo boxes

jwcolby jwcolby at colbyconsulting.com
Mon Mar 14 16:36:55 CDT 2011


Thanks Gustav,
I don't know why you are taking me into code in code.

I have data connections.  This particular data connection has a single view that allows me to write 
into a table.  It has a view that filters a combo.

I drag and drop the view for the table onto the form.  I have told that table object that specific 
fields are combos so when I drag it out, I have combos (for the FKs - PKs from other tables).  I am 
trying to set up that combo to

1) Display a text value
2) Contain the PKID from the table the combo draws from
3) When I select an item, place the selected ID into the field the combo is bound to.

It seems this is a normal daily requirement in building forms, and shouldn't be rocket science (or 
going into code).

Actually I have a kinda sorta related question.

When I look at server explorer I see a data connection object, however there are two objects 
underneath, neither of which are for this solution.  Furthermore the objects under the data 
connection are things found on an entirely unrelated server, for other projects that I did work on 
in the past.

The question then is why is the server explorer choosing to display objects out on a virtual machine 
instead of the database that was originally selected for this solution?

We have done a ton of coding sql server stuff here but we have not yet learned how to do this bound 
form stuff.

I am trying to build a timeclock kind of thing.  Very simple, a few combos to select (default values 
in most cases) the developer, the product, the type of work, then a couple of fields for date time 
values for start / stop work.  The table the data is going into has a few thousand records already 
in it, the combos pull from tables with 20-30 records etc.  It seems (to me) that just using bound 
forms is a natural for this.

A long time ago I made this (kind of thing) work.  I remember that getting the properties of the 
combo set correctly was non-intuitive but once I figured it out it just worked.



John W. Colby
www.ColbyConsulting.com

On 3/14/2011 3:02 PM, Gustav Brock wrote:
> Hi John
>
> If you (typically) have this in InitializeComponent() of the .Designer.cs file:
>
>              // comboBoxCountry
>              //
>              this.comboBoxCountry.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
>              this.comboBoxCountry.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
>              this.comboBoxCountry.BackColor = System.Drawing.Color.LavenderBlush;
>              this.comboBoxCountry.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.mediaBindingSource, "CountryId", true));
>              this.comboBoxCountry.DisplayMember = "Id";
>              this.comboBoxCountry.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
>              this.comboBoxCountry.Font = new System.Drawing.Font("Segoe UI", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
>              this.comboBoxCountry.ForeColor = System.Drawing.Color.DarkMagenta;
>              this.comboBoxCountry.FormattingEnabled = true;
>              this.comboBoxCountry.ItemHeight = 13;
>              this.comboBoxCountry.Location = new System.Drawing.Point(21, 136);
>              this.comboBoxCountry.Name = "comboBoxCountry";
>              this.comboBoxCountry.Size = new System.Drawing.Size(117, 21);
>              this.comboBoxCountry.Sorted = true;
>              this.comboBoxCountry.TabIndex = 7;
>              this.comboBoxCountry.ValueMember = "Id";
>
> You can do something like this in the Load event:
>
>              this.comboBoxCountry.SelectedValueChanged += (oo, ee) =>  ChangeCountrySelection();
>
> Then:
>          private void FillComboBoxCountryCode()
>          {
>              // Order the country names of countryDataTable.
>              string filterExpression = "";
>              string sortExpression = "CountryNameUK asc";
>              DataRow[] countryList = _countryDataTable.Select(filterExpression, sortExpression);
>
>              this.comboBoxCountry.DataSource = countryList;
>              this.comboBoxCountry.ValueMember = "Id";
>              this.comboBoxCountry.DisplayMember = "CountryNameUK";
>          }
>
> And:
>          private void ChangeCountrySelection()
>          {
>              if (this.comboBoxCountry.SelectedValue != null)
>              {
>                  _countryId = Convert.ToInt32(this.comboBoxCountry.SelectedValue);
>                  this.comboBoxPostalCode.Text = String.Empty;
>                  // Read postal codes of selected country.
>                  FillComboBoxPostalCode();
>              }
>          }
>
> This is pretty standard using tableadapters and bindingnavigator. Nothing fancy.
>
> /gustav
>
>
>>>> jwcolby at colbyconsulting.com 14-03-2011 19:16>>>
> I am trying to set up a combo box bound to a field in the form such that the combo populates with
> the PKID and a text field, and when the form opens, selecting something from the combo drops the
> PKID into the field in the form.
>
> I am not getting what to set the properties of the combo box to.  I am pulling the ID and the string
> to display into the combo but I cannot cause the combo to store the id and release control to the
> next control on the form.



More information about the dba-VB mailing list