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. Generic List

Generic List

Scheduled Pinned Locked Moved C#
wpfwcfquestion
6 Posts 4 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.
  • S Offline
    S Offline
    spankyleo123
    wrote on last edited by
    #1

    Hi , Is it possible to add a blank generic item to the List before binding it to the datasource? I hope this makes sense. Thanks,

    P 1 Reply Last reply
    0
    • S spankyleo123

      Hi , Is it possible to add a blank generic item to the List before binding it to the datasource? I hope this makes sense. Thanks,

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      It depends on what type your List is. Do you mean that you want to add a blank item to something like a combobox? If so, and you are using Win Forms or ASP.NET, you can bind to your data source, then insert the blank to the top of the list afterwards using something like myCombo.Items.Insert(" ", " ");

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      S 1 Reply Last reply
      0
      • P Pete OHanlon

        It depends on what type your List is. Do you mean that you want to add a blank item to something like a combobox? If so, and you are using Win Forms or ASP.NET, you can bind to your data source, then insert the blank to the top of the list afterwards using something like myCombo.Items.Insert(" ", " ");

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        S Offline
        S Offline
        spankyleo123
        wrote on last edited by
        #3

        yes thats right I want to add a blank item to the combobox. I have tried the

        combobox.items.insert("","")

        but am getting the error "Cannot add items to the combobox when a datasource is assigned". Using WINFORMS So I thought of creating a generic list of type T and pass a new instance of the item T to the list before the List is bound to the datasource.

        private static List<T> CreateList<T>(List<T> data)
        {
        //create new generic item (blank)

                 List<T> list = new List<T>();
                // list.Add(); add new generic item here
                 list.AddRange(data);
        
                return list;
            }
        

        I hope this makes sense.

        modified on Friday, February 26, 2010 6:26 AM

        P P S 3 Replies Last reply
        0
        • S spankyleo123

          yes thats right I want to add a blank item to the combobox. I have tried the

          combobox.items.insert("","")

          but am getting the error "Cannot add items to the combobox when a datasource is assigned". Using WINFORMS So I thought of creating a generic list of type T and pass a new instance of the item T to the list before the List is bound to the datasource.

          private static List<T> CreateList<T>(List<T> data)
          {
          //create new generic item (blank)

                   List<T> list = new List<T>();
                  // list.Add(); add new generic item here
                   list.AddRange(data);
          
                  return list;
              }
          

          I hope this makes sense.

          modified on Friday, February 26, 2010 6:26 AM

          P Offline
          P Offline
          Paulo Zemek
          wrote on last edited by
          #4

          If you don't know what is the blank value in advance, you can't do it the way you are trying. You can, for example, add a "" (or string.Empty) in a list of strings, but this will not work for other types. A possible solution is to create a generic "wrapper". For example:

          public struct MyWrapper<T>
          {
          public MyWrapper(T value)
          {
          _value = value;
          _hasValue = value != null;
          }

          private T _value;
          public T Value
          {
          get
          {
          return _value;
          }
          }

          private bool _hasValue;
          public bool HasValue
          {
          get
          {
          return _hasValue;
          }
          }

          public override string ToString()
          {
          if (!_hasValue)
          return "";

          return \_value.ToString();
          

          }
          }

          As this wrapper is a struct, it can be initialized by the default constructor, in which case the _hasValue is false, so the ToString will return blank. If you initialize it with null (considering T is a reference type) it will do the same. So, you can create a List<MyWrapper<T>> and add the first value as new MyWrapper<T> and then add a wrapper value for each value of the original list. But, if the original list is changed, this list will not be. *I didn't test the code, so maybe there is an error, but I think you can get the idea. You can also go even further and create an IList wrapper over another IList. It is a big work, but: You can always return Count as Count + 1. When requested for item 0, you always return the empty wrapper value. When requested for any other item, you return a wrapper over the real list item at (index-1) [after all, requesting item 1 means item 0 in the real list]. The advantage is that changing any item in the real data-source will be reflected in your wrapper data-source also, but it is a much more complex task.

          1 Reply Last reply
          0
          • S spankyleo123

            yes thats right I want to add a blank item to the combobox. I have tried the

            combobox.items.insert("","")

            but am getting the error "Cannot add items to the combobox when a datasource is assigned". Using WINFORMS So I thought of creating a generic list of type T and pass a new instance of the item T to the list before the List is bound to the datasource.

            private static List<T> CreateList<T>(List<T> data)
            {
            //create new generic item (blank)

                     List<T> list = new List<T>();
                    // list.Add(); add new generic item here
                     list.AddRange(data);
            
                    return list;
                }
            

            I hope this makes sense.

            modified on Friday, February 26, 2010 6:26 AM

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            Here's a quick sample that should work:

            private void BindToComboBox()
            {
            List<string> items = new List<string>();
            items.Add("Hello");
            items.Add("Hello 1");
            items.Add("Hello 2");
            items.Add("Hello 3");
            items.Add("Hello 4");

            cboList.Items.AddRange(items.ToArray());
            cboList.Items.Insert(0, " ");
            

            }

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            1 Reply Last reply
            0
            • S spankyleo123

              yes thats right I want to add a blank item to the combobox. I have tried the

              combobox.items.insert("","")

              but am getting the error "Cannot add items to the combobox when a datasource is assigned". Using WINFORMS So I thought of creating a generic list of type T and pass a new instance of the item T to the list before the List is bound to the datasource.

              private static List<T> CreateList<T>(List<T> data)
              {
              //create new generic item (blank)

                       List<T> list = new List<T>();
                      // list.Add(); add new generic item here
                       list.AddRange(data);
              
                      return list;
                  }
              

              I hope this makes sense.

              modified on Friday, February 26, 2010 6:26 AM

              S Offline
              S Offline
              Searril
              wrote on last edited by
              #6

              "Cannot add items to the combobox when a datasource is assigned" You need to add the blank row to a datatable before you bind it to the combobox. This is off the top of my head so will need debugged obviously but you will get the idea:

              SqlConnection MyConnection = new SqlConnection(PublicVars.ConnectionString);
              MyConnection.Open();

                          SqlDataAdapter sqlDA1 = new SqlDataAdapter("SELECT ItemID FROM some\_items", MyConnection);
                          DataSet sqlDS = new DataSet();
              
                          sqlDA1.Fill(sqlDS, "some\_items");
                          DataTable tblItems= sqlDS.Tables\["some\_items"\];
              
                          DataRow NewRow = tblItems.NewRow();
                          NewRow\[0\] = "-- Click To Select --";
                          tblItems.Rows.InsertAt(NewRow, 0);
              
                          cboItems.DataSource = tblItems;
                          cboItems.DisplayMember = "ItemID";
                          cboItems.ValueMember = "ItemID";
                          cboItems.SelectedIndex = 0;
              
                          MyConnection.Dispose();
              

              You should get the idea.

              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