none
how to Bound combox VS C# ? RRS feed

  • Question

  • 1- i am working in mobile application program and i have form contain combox i need to make this combox take column from the database table

    2-i tried this code

     

    DataBaseDataSet ds = new DataBaseDataSet();

     

     

    DataBaseDataSet.Favorities1DataTable dt = new DataBaseDataSet.Favorities1DataTable();

     

    foreach (DataRow dr in dt)

     

    {

    comboBox1.Items.Add(dr[1].ToString());

     

    }

    favorities1TableAdapter1.Fill(dt);

     

    it is work fine but take veeeeeeeeeeeeeeeeeery much time to load...

    and when i tried to set the combox datasource and display member and value member by wizard, i got nothing in combox

    any additional steps ....

    Wednesday, June 9, 2010 6:47 PM

Answers

  • no stpes required except

    Assign DataSource

    Set DisplayMember,SetValueMember

    Can you please try to set it using Code.

     private void Form1_Load(object sender, EventArgs e)
        {
          var lsts = new List<test>();
          lsts.Add(new test() { A = 2, B = 3 });
          lsts.Add(new test() { A = 2, B = 3 });
          lsts.Add(new test() { A = 2, B = 3 });
          lsts.Add(new test() { A = 2, B = 3 });     
          comboBox1.DisplayMember = "A";
          comboBox1.ValueMember = "B"; 
          comboBox1.DataSource = lsts;
    
        }
    
        public class test
        {
          public int A { get; set; }
          public int B { get; set; }
        }

    Regards
    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Wednesday, June 9, 2010 7:18 PM
  • I have changed code using DataTable and it is still working.

          DataTable dt = new DataTable();
          dt.Columns.Add("A");
          dt.Columns.Add("B");
          dt.Rows.Add(2, 3);
          dt.Rows.Add(4, 5);
          dt.Rows.Add(6, 7);          
          comboBox1.DataSource = dt;
          comboBox1.DisplayMember = "A";
          comboBox1.ValueMember = "B"; 

    Regards
    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, June 10, 2010 3:07 AM

All replies

  • no stpes required except

    Assign DataSource

    Set DisplayMember,SetValueMember

    Can you please try to set it using Code.

     private void Form1_Load(object sender, EventArgs e)
        {
          var lsts = new List<test>();
          lsts.Add(new test() { A = 2, B = 3 });
          lsts.Add(new test() { A = 2, B = 3 });
          lsts.Add(new test() { A = 2, B = 3 });
          lsts.Add(new test() { A = 2, B = 3 });     
          comboBox1.DisplayMember = "A";
          comboBox1.ValueMember = "B"; 
          comboBox1.DataSource = lsts;
    
        }
    
        public class test
        {
          public int A { get; set; }
          public int B { get; set; }
        }

    Regards
    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Wednesday, June 9, 2010 7:18 PM
  • thx mr nayan :)

    but i still have the same problem when i try to set it by code the combox is empty

    hint : i have data table in my databasedataSet which genrated autmaticaly when i added the database

    i use this code to bound by code :

    any wrong in my code?

     

    DataBaseDataSet.FavoritiesDataTable fd = new DataBaseDataSet.FavoritiesDataTable();

    comboBox1.DataSource = fd;

    comboBox1.DisplayMember =

    "Name";

    comboBox1.ValueMember =

    "ID";

    and thx again

    Wednesday, June 9, 2010 9:34 PM
  • I have changed code using DataTable and it is still working.

          DataTable dt = new DataTable();
          dt.Columns.Add("A");
          dt.Columns.Add("B");
          dt.Rows.Add(2, 3);
          dt.Rows.Add(4, 5);
          dt.Rows.Add(6, 7);          
          comboBox1.DataSource = dt;
          comboBox1.DisplayMember = "A";
          comboBox1.ValueMember = "B"; 

    Regards
    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, June 10, 2010 3:07 AM
  • I have changed code using DataTable and it is still working.

       DataTable dt = new DataTable();
    
       dt.Columns.Add("A");
    
       dt.Columns.Add("B");
    
       dt.Rows.Add(2, 3);
    
       dt.Rows.Add(4, 5);
    
       dt.Rows.Add(6, 7);     
    
       comboBox1.DataSource = dt;
    
       comboBox1.DisplayMember = "A";
    
       comboBox1.ValueMember = "B"; 
    
    

    Regards
    Nayan Paregi (MCTS)
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    speciaaal thxxxxxxx :) it is work fine
    Thursday, June 10, 2010 7:56 PM