Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Determine Key Of Selected Combo Item
Message
From
11/11/2008 00:47:44
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01360014
Message ID:
01361017
Views:
14
I'm assuming WinForms. So far, Tim has come the closest with the correct answer, but I'd like to elaborate. Consider the following code:
private void Form1_Load(object sender, System.EventArgs e)
{
    // this simply fills the table that is the source of the combo
    this.FillCodesData();

    this.cboCode.DataSource    = this.MyCodesDataTable;
    this.cboCode.DisplayMember = "Description";
    this.cboCode.ValueMember   = "Code";
    this.cboCode.SelectedIndex = -1;
    this.cboCode.DataBindings.Add("SelectedValue", this.MyDataTable, "MyDataCode");

    // you might also want this, although it's not necessary unless you have extra stuff to process
    this.cboCode.SelectedIndexChanged += new System.EventHandler(this.cboCode_SelectedIndexChanged);

}
What the above does is to set the DataSource of the Combo to a DataTable that contains the set of data you wish to display in the Combo's dropdown list. It also DataBinds the combo to the DataTable where you're actually updating data when the user selects a value from the Combo. Nothing special needs to be done in the SelectedIndexChanged event handler, unless you need to do something with the selected item other than stick it in a table (it will do that automatically because you databound the "SelectedValue").

Does this help any?

~~Bonnie






>I have a combo bound to a data set. The first column of the data set it the PK.
>
>When the user selects an item, how do I determine the PK of the selected item?
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform