HOw to populate the combo box in C#?

  • Thread starter Thread starter Guest
  • Start date Start date
Use a Dataset object, via a DataAdapter (the SELECT statement is one of the
properties of the data adapter), and bind the combo box to a specific column
in the dataset.

Tom Dacon
Dacon Software Consulting
 
Depends on how you are loading the data from the select query.

If you are loading the data into a dataset, then you can use databinding
to populate the combobox

combo.DataSource = dataset["Table"];
combo.DisplayMember = "ColumnName";

If you are loading the data into an ArrayList then

combo.DataSource = arrayListVariable;
combo.DisplayMember = "FieldName";

if you are going to populate it manually by useing a DataReader then you
can use Combo.Items.Add() method.

You can also look at these quickstarts to get started
http://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsData.aspx

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 
This my code "When I debug through the code connection is said - Undefined
Value" Could you please tell me why?

String strSQL;

strSQL = "SELECT F.FollowUpPerson FROM tblFollowUpPerson F INNER JOIN
tblFollowUpPersonClient C ON F.FollowUpPersonID = C.FollowUpPersonID WHERE
C.ClientKey = '" + this.cboClient.SelectedValue + "' ";

ds = mg.GetDataSet(strSQL,"userList"); //Definition is later



cmbUserList.DataSource = ds;
cmbUserList.DisplayMember = "FollowUpPerson";
cmbUserList.ValueMember = "FollowUpPerson";

public DataSet GetDataSet(string sqlQuery, string name)
{
SqlConnection conn = null;

if ( m_connInfo == null )
return null;

try
{
if ( persistTrans == null )
conn = new SqlConnection(m_connInfo.DBConnString);
else
conn = persistTrans.Connection;

m_dataAdapter = new SqlDataAdapter(sqlQuery, conn);
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(m_dataAdapter);
DataSet dataSet = new DataSet(name);
m_dataAdapter.SelectCommand.CommandTimeout = m_connInfo.CommandTimeout;
m_dataAdapter.Fill(dataSet);

return dataSet;
}
catch(Exception e)
{
logger.Write(sqlQuery);
logger.WriteLine(e);
return null;
}
finally
{
if ( persistTrans == null && conn != null )
conn.Close();
}
}

Sijin Joseph said:
Depends on how you are loading the data from the select query.

If you are loading the data into a dataset, then you can use databinding
to populate the combobox

combo.DataSource = dataset["Table"];
combo.DisplayMember = "ColumnName";

If you are loading the data into an ArrayList then

combo.DataSource = arrayListVariable;
combo.DisplayMember = "FieldName";

if you are going to populate it manually by useing a DataReader then you
can use Combo.Items.Add() method.

You can also look at these quickstarts to get started
http://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsData.aspx

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
I have a combo box (C#)that I want to populate from a SELECT query How to do
this?
 
Back
Top