Filling up a combobox

  • Thread starter Thread starter Hugo Lefevre
  • Start date Start date
H

Hugo Lefevre

dear,

I have a form (frmlogonaccounts.cs) with a combobox, which I want to fill up
with the first column of a dataset. The dataset is created in the class
Login. My problem is the next : I make a new login in the form loginaccount
which create a new login and give me a dataset (created by class login),
which I can see in my datagrid. But I want to fill up my combobox with only
the name of the logon and I do this with an foreach. But when I see the
result the combobox is filled up with only the name of the column, not with
the data in the dataset (as I said I see the data in the datagrid: so the
dataset is filled up).

What do I wrong ? Below you find the code in class Login and class
frmlogonaccount

Thank you very much.

Hugo



code Login :

public Login(out DataSet ds)
{
SqlSelectie = "select * from Login; ";
Database db = new Database();
SqlConnection conn = db.Opent();
SqlCommand cmd = new SqlCommand(SqlSelectie, conn);
SqlDataAdapter da = new SqlDataAdapter(SqlSelectie, conn);
ds = new DataSet();
da.Fill(ds, "Login");
conn.Close();
}

code frmlogonaccount :

DataSet ds = new DataSet("Login");
Login SqlLogin = new Login(out ds);
dataGrid1.SetDataBinding(ds, "Login");
///the next statement gives me the name of the column NOT the data
foreach (DataRow dr in ds.Tables["Login"].Rows)
cboListLogon.Items.Add(dr.Table.Columns[0]);
 
Hi Hugo,

Hugo Lefevre said:
dear,

I have a form (frmlogonaccounts.cs) with a combobox, which I want to fill up
with the first column of a dataset. The dataset is created in the class
Login. My problem is the next : I make a new login in the form loginaccount
which create a new login and give me a dataset (created by class login),
which I can see in my datagrid. But I want to fill up my combobox with only
the name of the logon and I do this with an foreach. But when I see the
result the combobox is filled up with only the name of the column, not with
the data in the dataset (as I said I see the data in the datagrid: so the
dataset is filled up).

What do I wrong ? Below you find the code in class Login and class
frmlogonaccount

Thank you very much.

Hugo



code Login :

public Login(out DataSet ds)
{
SqlSelectie = "select * from Login; ";
Database db = new Database();
SqlConnection conn = db.Opent();
SqlCommand cmd = new SqlCommand(SqlSelectie, conn);
SqlDataAdapter da = new SqlDataAdapter(SqlSelectie, conn);
ds = new DataSet();
da.Fill(ds, "Login");
conn.Close();
}

code frmlogonaccount :

DataSet ds = new DataSet("Login");
Login SqlLogin = new Login(out ds);
dataGrid1.SetDataBinding(ds, "Login");
///the next statement gives me the name of the column NOT the data
foreach (DataRow dr in ds.Tables["Login"].Rows)
cboListLogon.Items.Add(dr.Table.Columns[0]);

It should be:
cboListLogon.Items.Add((string)dr[0]);
instead.
Also you might consider databinding to combobox entire table
ds.Tables["Login"].
Just set DisplayMember = "columnname" and DataSource = ds.Tables["Login"];
 
Hi Hugo,

You can try databinding the login name column to your
combobox by using the DisplayMember and ValueMember
properties. Here's a code snippet:

cboListLogon.DataSource=ds.Tables["Login"];
cboListLogon.DisplayMember="loginname";
cboListLogon.ValueMember="loginname";

Regards,
Aravind C
 
Back
Top