How to Bind ComboBox1 with DataSet1 ?
-
I want to bind a ComboBox with a DataSet by supplying a datasource and also want to specify ComboBox.valueMember and ComboBox.DisplayMember in a windows form in C#. What i m trying is below the piece of code but i m not getting the required result; ComboBox1.DataSource = DataSet1; ComboBox1.DisplayMember = "StudentName"; ComboBox1.ValueMember = "StudentID"; How to Bind ComboBox1 with DataSet1 ? Please correct the above code snippt.
-
I want to bind a ComboBox with a DataSet by supplying a datasource and also want to specify ComboBox.valueMember and ComboBox.DisplayMember in a windows form in C#. What i m trying is below the piece of code but i m not getting the required result; ComboBox1.DataSource = DataSet1; ComboBox1.DisplayMember = "StudentName"; ComboBox1.ValueMember = "StudentID"; How to Bind ComboBox1 with DataSet1 ? Please correct the above code snippt.
Try this snippet. I think you simply didn't bind to the Table, rather than the data set.
// Set up the DataSet
DataSet ds = new DataSet();// Set up the Data Table
DataTable t = new DataTable("Students");
t.Columns.Add("StudentID", typeof(int));
t.Columns.Add("StudentName", typeof(string));ds.Tables.Add(t);
// Fake Data
for(int i = 0; i < 5; i++)
{
DataRow r = t.NewRow();
r["StudentID"] = i;
r["StudentName"] = "Test Student " + i.ToString();
t.Rows.Add(r);
}// Bind to the Combo Box
comboBox1.DataSource = ds.Tables[0];
// NOTE that my data source is the table, not the datasetcomboBox1.ValueMember = "StudentID";
comboBox1.DisplayMember = "StudentName";Access the selected value:
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
MessageBox.Show(comboBox1.SelectedValue.ToString());
} -
I want to bind a ComboBox with a DataSet by supplying a datasource and also want to specify ComboBox.valueMember and ComboBox.DisplayMember in a windows form in C#. What i m trying is below the piece of code but i m not getting the required result; ComboBox1.DataSource = DataSet1; ComboBox1.DisplayMember = "StudentName"; ComboBox1.ValueMember = "StudentID"; How to Bind ComboBox1 with DataSet1 ? Please correct the above code snippt.