Hello Omar,
You can do something like this:
try
{
DataSet ds=new DataSet();
SqlDataAdapter adapter=new SqlDataAdapter("select * from
jobs","server=localhost;database=pubs;uid=sa;pwd=");
adapter.Fill(ds);
comboBox1.DataSource=ds.Tables[0];
comboBox1.DisplayMember="job_desc";
comboBox1.ValueMember="job_id";
}
catch(Exception ex)
{
MessageBox.Show(ex.Message );
}
I use the default database of SqlServer and use the job_desc column as the
display item of comboBox, job_id column as the value item of the comboBox.
You can get the current selected member of these 2 column value by
comboBox1.SelectedText and comboBox1.SelectedValue.
Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| Reply-To: "Omar" <none>
| From: "Omar" <none>
| Subject: problem binding comboBox to dataset
| Date: Wed, 8 Oct 2003 17:51:35 -0500
| Lines: 36
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <
[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 66-50-71-153.prtc.net 66.50.71.153
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:190027
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| When I try to databind my comboBox (specifically field "emplcode") to a
| filled dataset , the contents of the comboBox displays a bunch of
| "System.Data.DataRowView". I assume the amount of times
| "System.Data.DataR..." is displayed inside the combobox is the amount of
| records in the dataset. On the other hand, if my query is "select emplcode
| from payemployee", the databind will work fine (but I don't want to limit
| the dataset to one field). Any help is appreciated. This is the code:
|
| DataSet DS_Employees = new DataSet();
|
| SqlConnection SQLConn = new SqlConnection("Data Source=localhost;
Integrated
| Security=SSPI;" +
|
| "Initial Catalog=payroll");
|
| string strThisQuery = "select * from payemployee"; //If the query were
| "select emplcode from payemployee" it works
|
| SqlDataAdapter DA_empl = new SqlDataAdapter (strThisQuery, SQLConn);
|
| DA_empl.Fill(DS_Employees, "payemployee"); //Do I need the 2nd parameter?
|
| SQLConn.Close();
|
| comboBox_emplcode.DataSource = DS_Employees.Tables["payemployee"];
|
| comboBox_emplcode.DisplayMember = "emplcode"; //I also tried
| comboBox_emplcode.DisplayMember = "payemployee.emplcode";
|
|
|
| Thanks again,
|
| Omar
|
|
|