Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically assigning a combo box

Status
Not open for further replies.

mbde

Programmer
Mar 14, 2005
55
US
Sounds easy… then why can’t I do it

I have on my windows form 2 combo boxes and a datagrid

The first combo box is static just a list of tables – cboTables

After a selection is made on the first combo box, populate the second combo box, cboField, where the displaymember is different based on the table and valuemember is an guid – id

And then finally based on the selection in cboField, display a record in the datagrid, dg1

So on btnclick after the first box is selected (I change this to a button to test something, but ideally on selectedindexchage of cboTables) this is the code


string fieldName = "";

lblProcessing.Text = "Getting Records . . . ";

switch (this.cboTables.Text)
{
case "Accounts":
fieldName = "Name";
break;
case "Companies":
fieldName = "Company";
break;
case "Contracts":
fieldName = "LastName";
break;
}


if (fieldName != "")
{

this. cboField.DisplayMember = fieldName; //#1
this. cboField.ValueMember = "Id"; //#1
this. cboField.DataSource = getRecordsReturnOneField(this. cboTables.Text, fieldName);

this. cboField.Refresh(); // #2


Side note, if I could get at the query I would alias the main display column to be all the same name so the switch statement is not needed, but I do not have access to the SP.

Now on Selected index change of cboField I have the following code

private void cboRecords_SelectedIndexChanged(object sender, System.EventArgs e)
{

if (cboRecords.SelectedIndex != -1)
{
lblProcessing.Text = "Getting Record . . . ";

dataGrid1.DataSource = getRecordByID(cboTables.Text, "Id", cboField.SelectedValue.ToString());


lblProcessing.Text = "";

}
}

Ok still with me. Here is the problem, first time through everything works great. If I make a new selection in cboTables, the display value in cboField is System.Data.DataRowView, amazingly even though you can see the field value the datagrid updates correctly. What I have notice is that cboField.Displaymember does not change the second time through. The third time through (if I select the same value) everything works fine. So I added line #2 to do a refresh, nothing changed.

So in reading the MSDN, in their example they set the Displaymember and ValueMember after the setting of the DataSource. So I moved lines #1 after the datasource. The problem I have with this is that, selectedIndexChange gets fired 3 times (once on DataSource, once on ValueMember, and again on DisplayMember) and All three times cboFields.SelectedValue.ToString() = “System.Data.DataRowView” and SelectedIndex = 0.

What is the proper way to set up this interface.


Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top