Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Get PK of Selected Combo Item
Message
From
02/01/2009 21:45:31
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01370790
Message ID:
01370791
Views:
17
>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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform