Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to Bind ComboBox1 with DataSet1 ?

How to Bind ComboBox1 with DataSet1 ?

Scheduled Pinned Locked Moved C#
csharptutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    majidbhutta
    wrote on last edited by
    #1

    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.

    S N 2 Replies Last reply
    0
    • M majidbhutta

      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.

      S Offline
      S Offline
      Susan Hernandez
      wrote on last edited by
      #2

      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 dataset

      comboBox1.ValueMember = "StudentID";
      comboBox1.DisplayMember = "StudentName";

      Access the selected value:

      private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
      {
      MessageBox.Show(comboBox1.SelectedValue.ToString());
      }

      1 Reply Last reply
      0
      • M majidbhutta

        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.

        N Offline
        N Offline
        n10sive
        wrote on last edited by
        #3

        you need to add more qualification. ie. ComboBox1.DataSource = DataSet1.Table or ComboBox1.DataSource = DataSet1; ComboBox1.DisplayMember = "Table.StudentName"; ComboBox1.ValueMember = "Table.StudentID"; Cheers

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups