none
[C#] How to bind a boolean Value to a comboBox

    Question

  • Hi Folks.

    I'm new to C#.

    I bind successfully some ILists to some comboboxes in my project.

    But no i have to bind a boolean value to a combobox...how could i handle this?

    Thanks for all Ideas.

    Greetings.

    Wednesday, May 26, 2010 12:22 PM

Answers

  • A boolean should not be bound to a combo box (imho) . A combo box control is meant to be one item out of many. For booleans you should use the check box as this is moe natural for the end user. 
    • Marked as answer by Helen Zhou Friday, May 28, 2010 5:26 AM
    Thursday, May 27, 2010 6:01 AM

All replies

  • A boolean should not be bound to a combo box (imho) . A combo box control is meant to be one item out of many. For booleans you should use the check box as this is moe natural for the end user. 
    • Marked as answer by Helen Zhou Friday, May 28, 2010 5:26 AM
    Thursday, May 27, 2010 6:01 AM
  • Ah, yes you're right.

    I'll do that your way. Thanks!

    Thursday, May 27, 2010 6:50 AM
  •     public SqlConnection GetConnection()
        {
          string sqlstr = "Data Source=172.18.39.48;Database=SuperMarket;User Id=sa;PWD=";
          SqlConnection sqlcon = new SqlConnection(sqlstr);
          return sqlcon;
        }
    
        public DataSet GetDataSet(string sqlstr, string table_str)
        {
          SqlConnection sqlcon = this.GetConnection();
          SqlDataAdapter sqlda = new SqlDataAdapter(sqlstr, sqlcon);
          DataSet ds = new DataSet();
          sqlda.Fill(ds, table_str);
          return ds;
        }
    
         //ComboBox DataBinding
        public void ComboboxBind(string sqlstr, string table_str, string tb_member, ComboBox cbox)
        {
          DataSet ds = this.GetDataSet(sqlstr, table_str);
          cbox.DataSource = ds.Tables[table_str];
          cbox.DisplayMember = tb_member;
        }
    
    
        private void Form2_Load(object sender, EventArgs e)
        {
          this.ComboboxBind("SELECT distinct isValid FROM test", "test", "isValid", comboBox1);
        }

    If (My Answer) Please mark the replies as answer. Thanks; Else Thank you all the same; My Code Blog:http://nauhil.wordpress.com/
    Thursday, May 27, 2010 8:44 AM
  • Sorry to resurrect this, I just happened to be searching on how to populate a combobox which represents a boolean and stumbled onto this.

    If I'm reading Huan Li's code correctly, it obtains the current distinct values for a particular database field and uses these to populate the combobox. Depending on the application, this may or may not be desired - I would suggest it is not desirable when the field represents an enumeration in code. For example, not all members of the enumeration may be represented in the database.

    I got locked into the development pattern of the form I'm modifying which has only text boxes and combo boxes. I suppose a check box may be more appropriate - but it's interesting to note the Microsoft generally uses a combo box as the default entry mechanism for booleans in property windows (e.g. in the VS.NET IDE).

    Sincerely,

    mszil

    Tuesday, November 29, 2011 5:50 PM