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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combo Box and a Table Binding to Variables

Status
Not open for further replies.

rdrunner400

Programmer
Feb 12, 2007
17
GB
Hi All,

I have a combo box that I am populating as per the following code.

It is coming from a table that has 8 fields per record and in the combo box i am making the first field of the record appear in the Combo Box.

My question is as follows. If I have 50 records in the underlying table I want to be able to select any of the names in the first column of the table and get the contents of the other 7 fields in the row from the table and store them in global variables.

So if I have have selected Name 1 from the table which is showing in the combo box I would like to be able to assign the other 7 fields contents to constant variables to be used furhter in the program.


Here is the code I am using .......


this.cboCombo.Visible = true;

SqlConnection cs = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename='C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\Names.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True");

// Create the data apadter
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Names ORDER by Full_Name", cs);

// Create the Data Table
DataTable dt = new DataTable();

// Fill Data Table with Records
da.Fill(dt);

// Loop around the table and populate the combo box with records....

for (int i = 0; i < dt.Rows.Count; i++)
{
cboCombo.Items.Add(dt.Rows["Full_Name"]);
}

So at this point i have got the Full Names from the Table Names and i have loaded them into the Combobox and when i click on the combo box i would like to retreive the contents of the other 7 fields in the underlying table and place them into variables.

Is this possible ???

regards in advance

rdrunner
 
Instead of adding a string, you need to add a ListItem.
You will need to set the Value member of the List item to the Primary Key of your table.

Then in your selectedIndexChanged event you can use the SelectedValue to get the rest of the fields.

Lodlaiden

You've got questions and source code. We want both!
Oh? That? That's not an important password. - IT Security Admin (pw on whiteboard)
 
Hi,

Thanks for the idea - it works a treat ....

Regards

Rdrunner
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top