C# Fill comboBox with DB content
-
I want to Bind two Columns and populate a ComboBox with it in C#..My code is printing just one. the FirstName. Have tried concatenating the LastName with it but its not working. Can anyone show me how to go about it. Here is my code. Thanks
OleDbCommand cmd = new OleDbCommand("SELECT * FROM ADb", myCon);
OleDbDataAdapter d = new OleDbDataAdapter(cmd);
d.SelectCommand.CommandText = cmd.CommandText.ToString();DataTable dt = new DataTable(); d.Fill(dt); cmbNames.DataSource = dt; cmbNames.DisplayMember = "FirstName";
-
I want to Bind two Columns and populate a ComboBox with it in C#..My code is printing just one. the FirstName. Have tried concatenating the LastName with it but its not working. Can anyone show me how to go about it. Here is my code. Thanks
OleDbCommand cmd = new OleDbCommand("SELECT * FROM ADb", myCon);
OleDbDataAdapter d = new OleDbDataAdapter(cmd);
d.SelectCommand.CommandText = cmd.CommandText.ToString();DataTable dt = new DataTable(); d.Fill(dt); cmbNames.DataSource = dt; cmbNames.DisplayMember = "FirstName";
-
I want to Bind two Columns and populate a ComboBox with it in C#..My code is printing just one. the FirstName. Have tried concatenating the LastName with it but its not working. Can anyone show me how to go about it. Here is my code. Thanks
OleDbCommand cmd = new OleDbCommand("SELECT * FROM ADb", myCon);
OleDbDataAdapter d = new OleDbDataAdapter(cmd);
d.SelectCommand.CommandText = cmd.CommandText.ToString();DataTable dt = new DataTable(); d.Fill(dt); cmbNames.DataSource = dt; cmbNames.DisplayMember = "FirstName";
-
I want to Bind two Columns and populate a ComboBox with it in C#..My code is printing just one. the FirstName. Have tried concatenating the LastName with it but its not working. Can anyone show me how to go about it. Here is my code. Thanks
OleDbCommand cmd = new OleDbCommand("SELECT * FROM ADb", myCon);
OleDbDataAdapter d = new OleDbDataAdapter(cmd);
d.SelectCommand.CommandText = cmd.CommandText.ToString();DataTable dt = new DataTable(); d.Fill(dt); cmbNames.DataSource = dt; cmbNames.DisplayMember = "FirstName";
One approach is to retrieve the data in the format you want:
OleDbCommand cmd = new OleDbCommand("SELECT id, FirstName + ' ' + LastName as Name FROM ADb", myCon);
OleDbDataAdapter d = new OleDbDataAdapter(cmd);
d.SelectCommand.CommandText = cmd.CommandText.ToString();DataTable dt = new DataTable();
d.Fill(dt);
cmbNames.DataSource = dt;
cmbNames.DisplayMember = "Name";I took a guess at the name of your ID column. You still need to select that if you use the combo for selecting things elsewhere.
-
I want to Bind two Columns and populate a ComboBox with it in C#..My code is printing just one. the FirstName. Have tried concatenating the LastName with it but its not working. Can anyone show me how to go about it. Here is my code. Thanks
OleDbCommand cmd = new OleDbCommand("SELECT * FROM ADb", myCon);
OleDbDataAdapter d = new OleDbDataAdapter(cmd);
d.SelectCommand.CommandText = cmd.CommandText.ToString();DataTable dt = new DataTable(); d.Fill(dt); cmbNames.DataSource = dt; cmbNames.DisplayMember = "FirstName";
-
One approach is to retrieve the data in the format you want:
OleDbCommand cmd = new OleDbCommand("SELECT id, FirstName + ' ' + LastName as Name FROM ADb", myCon);
OleDbDataAdapter d = new OleDbDataAdapter(cmd);
d.SelectCommand.CommandText = cmd.CommandText.ToString();DataTable dt = new DataTable();
d.Fill(dt);
cmbNames.DataSource = dt;
cmbNames.DisplayMember = "Name";I took a guess at the name of your ID column. You still need to select that if you use the combo for selecting things elsewhere.
Thanks BobJanova. Thanks Everyone...