Click to See Complete Forum and Search --> : ComboBox


dasm
May 1st, 2008, 10:54 AM
Trying to load a combobox dynamically with the following piece of code.
count gives exact number of rows !!, but the combobox does not show anything..!!.
Any help is greatly appreciated.

public void loadComboBox(OdbcConnection con)
{
con = getConnection();
con.Open();

string str = "select distinct mgrCode from MgrDetail";

OdbcCommand nonqueryCommand = con.CreateCommand();
nonqueryCommand.CommandText = str;
nonqueryCommand.CommandType = CommandType.Text;
OdbcDataReader thisReader = nonqueryCommand.ExecuteReader();

DataSet ds = new DataSet();
DataTable dTable = new DataTable("TableA");
dTable.Load(thisReader);
ds.Tables.Add(dTable);
this.ComboBox1.DataSource = ds.Tables[0];
int count = this.ComboBox1.Items.Count; //count gives exact number of rows !!

con.Close();

}

jasonli
May 1st, 2008, 11:05 AM
You need to set DisplayMember and ValueMember for ComboBox. Like:ComboBox1.DisplayMember = FieldNameinDataTable;

dasm
May 1st, 2008, 12:08 PM
That worked..
Thanks a lot.