Windows Develop Bookmark and Share   
 index > Windows Forms General > Combobox with two columns
 

Combobox with two columns

Hello,

I have the following code:
      public void fillCombo (SqlConnection conn,ComboBox combo,String sproc,String code,string description,string year)
       {
                using (SqlCommand cmd2 = new SqlCommand(sproc))
                {
                    cmd2.CommandType = CommandType.StoredProcedure;

                    conn.Open();
                    cmd2.Connection = conn;

                    using (SqlDataReader rdr2 = cmd2.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        DataTable dt2 = new DataTable();
                        dt2.Columns.Add(code, typeof(decimal));
                        dt2.Columns.Add(description, typeof(string));
                        dt2.Columns.Add(year,typeof(int));
while (rdr2.Read()) { DataRow dr2 = dt2.NewRow(); dr2[0] = rdr2.GetDecimal(rdr2.GetOrdinal(code)); dr2[1] = rdr2.GetString(rdr2.GetOrdinal(description)); dr2[2] = rdr2.GetString(rdr2.GetOrdinal(year));

dt2.Rows.Add(dr2); } rdr2.Close(); combo.DataSource = dt2; combo.DisplayMember = description; combo.ValueMember = code; } } }
How can i add dr2[2] to ValueMember as well? I want the combobox to have two columns.

Thank you in advance.
  • Moved byTaylorMichaelLMVPFriday, September 18, 2009 1:34 PMWinForms related (From:Visual C# General)
  •  
dealwi8me  Friday, September 18, 2009 10:29 AM
Comboboxes do not support multiple columns in Windows. You need to switch to a better control, write your own or use a third-party control that combines a combobox with a listview-like control. There are quite a few of them out there so Google and you'll get some links.

If you don't want to go through the hassle of a custom control then the alternative option is to ensure that the displayed text is formatted such that all the data appears in multiple columns. This would be accomplished by ensuring that the "first column" of data is always a consistent length. You could then append the "second column". The resultant string can then be added to the control and give the illusion of multiple columns.

BTW the ValueMember property is not for setting the displayed text but rather for setting the value (any value) to associate with each item. The DisplayMember property determines what property is used to display text to the user.

Michael Taylor - 9/18/09
http://p3net.mvps.org
TaylorMichaelL  Friday, September 18, 2009 1:34 PM
Comboboxes do not support multiple columns in Windows. You need to switch to a better control, write your own or use a third-party control that combines a combobox with a listview-like control. There are quite a few of them out there so Google and you'll get some links.

If you don't want to go through the hassle of a custom control then the alternative option is to ensure that the displayed text is formatted such that all the data appears in multiple columns. This would be accomplished by ensuring that the "first column" of data is always a consistent length. You could then append the "second column". The resultant string can then be added to the control and give the illusion of multiple columns.

BTW the ValueMember property is not for setting the displayed text but rather for setting the value (any value) to associate with each item. The DisplayMember property determines what property is used to display text to the user.

Michael Taylor - 9/18/09
http://p3net.mvps.org
TaylorMichaelL  Friday, September 18, 2009 1:34 PM

You can use google to search for other answers

Custom Search

More Threads

• Validation for E-mail address
• Form.Show() from background thread
• Button Event Not firing ( should be simple )
• problem: Drawing disappears!!
• RTF to HTML Conversion
• rotating a servo using c#
• isMdiContainer = true
• creating project similar to outlook
• Cross thread operation not valid.
• Drawing between components a la Access