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