Get corresponding column value

  • Thread starter Thread starter RP
  • Start date Start date
R

RP

I have a combo box and I am filling Employee Codes in it. I want to
retrieve employee first and last name when a user selects an employee
code from the combo box. Though it very easy with a single SQL Query,
I want to know if there is any other way to get this. I mean if I use
following query:

Select EmpCode, FirstName, LastName from <Table Name>

and use column(o) to fill the combo box. Can I store other values as
well in combo box to retrieve them when the user selects a code.
 
Hi,
You can create an employee class and fill the combo box with a set of
those.
if you overwrite the ToString() method so that it returns EmpCode then
the combox box will show that.

Use the Selected indexed changed event to get the selected item which
can be cast as an employee (if it is not already, I cant remember) and
all its properties accessed.
hth
bob
 
Hello, RP!

If you're talking about WinForms application - combobox control has Tag
property. This property can be used to store arbitrary data.

If employee's select resultset is not large, you can cache employee data
into a data structure ( dictionary with employee code as key) and put that
data structure
into control's Tag property. When user selects value from combobox you'll
perfrom search in the attached dictionary to retrieve Emp data.

OTOH if employee's select results set can be large enough other strategies
of data retrieval have to come into play.

--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com

You wrote on Tue, 16 Oct 2007 08:37:57 -0000:

R> I have a combo box and I am filling Employee Codes in it. I want to
R> retrieve employee first and last name when a user selects an employee
R> code from the combo box. Though it very easy with a single SQL Query,
R> I want to know if there is any other way to get this. I mean if I use
R> following query:

R> Select EmpCode, FirstName, LastName from <Table Name>

R> and use column(o) to fill the combo box. Can I store other values as
R> well in combo box to retrieve them when the user selects a code.
 
RP said:
I have a combo box and I am filling Employee Codes in it. I want to
retrieve employee first and last name when a user selects an employee
code from the combo box. Though it very easy with a single SQL Query,
I want to know if there is any other way to get this. I mean if I use
following query:

Select EmpCode, FirstName, LastName from <Table Name>

and use column(o) to fill the combo box. Can I store other values as
well in combo box to retrieve them when the user selects a code.

Yes you can. If it's databound it's pretty easy:

comboBox1.DataSource = dt;
comboBox1.DisplayMember = "EmpCode";
comboBox1.ValueMember = "EmpCode";

Then you can extract the DataRow/DataRowView (by default if you bind it,
it's a DataRowView) from the combobox like:

DataRowView row = (DataRowView)comboBox1.SelectedItem;
MessageBox.Show(row["EmpCode"].ToString() + ": " +
row["FirstName"].ToString() + " " +
row["LastName"].ToString());

If it's your own custom object, it's pretty much the same. SelectedItem
would be whatever you added via a data binding process or via the
comboBox1.Items.Add() method.


Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top