CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Bind ComboBox to Data and add a first item with text - is there an easier way?

702 Views
Copy Code Show/Hide Line Numbers
 
   public class ListUtils
   {
       /// <summary>
       /// Manual ComboBox binding and adding a first item
       /// 
       /// Assumes you're binding to an enumerable list of some sort
       /// and you're binding to Properties (not fields)
       /// </summary>
       public static void BindWithFirstItem(IEnumerable items, ComboBox listBox,
                                  string textField, string valueField,
                                  string firstItemText, object firstItemValue)
       {
           // We'll actually bind ListItem
           listBox.DisplayMember = "DisplayText";
           listBox.ValueMember = "Value";
 
           if (firstItemText != null)
           {
               ListItem li = new ListItem()
               {
                   DisplayText = firstItemText,
                   Value = firstItemValue
               };
               listBox.Items.Add(li);
           }
 
           foreach (object item in items)
           {
               PropertyInfo piText = null;
               PropertyInfo piValue = null;
 
               if (piText == null)
               {
                   piText = item.GetType().GetProperty(textField);
                   piValue = item.GetType().GetProperty(valueField);
               }
 
               ListItem li = new ListItem()
               {
                   DisplayText = piText.GetValue(item, null) as string,
                   Value = piValue.GetValue(item, null)
               };
               listBox.Items.Add(li);
           }
 
           if (listBox.Items.Count > 0)
               listBox.SelectedIndex = 0;
       }
   }
 
   /// <summary>
   /// Use this for actual binding
   /// </summary>
   public class ListItem
   {
       public string DisplayText { get; set; }
       public object Value { get; set; }
       public object Tag { get; set; }
   }
 
   // sample usage
   ProductList = new List<Product>()
   {
     new Product() { Sku = "WCONNECT", Descript="Web Connection", Id=1 },
     new Product() { Sku = "WWHELP", Descript="Help Builder", Id=2 },
     new Product() { Sku = "WEBTOOLKIT", Descript="Web Toolkit", Id=3 }, 
   };
 
   ListUtils.BindWithFirstItem(ProductList, this.lstProducts,  // combobox
                            "Descript", "Id", 
                            "Please choose one...", -1);
 
by Rick Strahl
  October 27, 2009 @ 3:46pm
Tags:
Description:
Trying to bind a list to data but have a first item added (like Choose one...) in a somewhat generic way.

by Bron    October 27, 2009 @ 4:08pm

That's pretty much the same way I've always done it, I set up a separate utility function to optionally add a first item. It's a bit of a pain with listViews....

Dropdown lists are a bit easier when it comes to that because if you are doing a simple binding to a list, etc you can add the "choose one" item in the ASPX code, if you like, set AppendDataBoundItems to true and then just do a datasource=xxlistxx, etc.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate