Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Get PK of Selected Combo Item
Message
De
02/01/2009 21:45:31
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01370790
Message ID:
01370791
Vues:
19
>Is this the best way to do this?

Not quite. First, the DataSource should be the table, not the DataSet:
cboClients.DataSource = oClients.Clients; // this is your DataTable, I assume
Then you want to set both a DisplayMember *and* a ValueMember. The ValueMember will be set to your PK:
cboClients.DisplayMember = "clientname";
cboClients.ValueMember = "ClientKey";
Now, as far as getting the Value that's been selected by the user, you can do it this way:
int iClientKey = (int)cboClients.SelectedValue;
Or, if you have DataBound your Combo, you don't have to do anything ... the value will automatically get put into the column you've data bound to.
cboClients.DataBindings.Add("SelectedValue", MyDataSet.MyTable, "MyKeyColumn");
Make sense?

~~Bonnie




>Is this the best way to do this?
>
>First, I'm loading a combo with a data set as follows:
>
>
>
>cboClients.DataSource = oClients; // oClients is a DataSet
>cboClients.DisplayMember = "Clients.clientname";
>cboClients.SelectedIndex = 0;
>
>
>
>Then, I get the PK of the selected item as follows:
>
>
>int  iIndex = cboClients.SelectedIndex;
>DataSet oClients = (DataSet)cboClients.DataSource;
>int iClientKey = Convert.ToInt32(oClients.Tables["Clients"].Rows[iIndex]["ClientKey"]);
>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform