DataSource containing null causes ComboBox to crash
I have selected myself headfirst in C # and .Net 2.0 using Linq and I have several problems debugging some of the problems, namely the following:
I have a ComboBox ( cmbObjects
) control . I want to populate a collection of objects obtained using Linq. I wrote a helper method to fill the List<T>
generic:
class ObjectProvider
{
public static List<T> Get<T>(bool includeNull) where T : class, new()
{
List<T> list = new List<T>();
LutkeDataClassesDataContext db = ConnectionManager.GetConnection();
IQueryable<T> objects = db.GetTable<T>().AsQueryable();
if (includeNull) list.Add(null);
foreach (T o in objects) list.Add(o);
return list;
}
public static List<T> Get<T>() where T : class, new()
{
return Get<T>(false);
}
}
I checked the results when calling a function with true or false value - List
contains correct values, when passed true
it contains null
as first value followed by other objects.
When assigned DataSource
to ComboBox
, however, the control simply refuses to display any items, including the value null
(not selectable):
cmbObjects.DataSource = ObjectProvider.Get<Car>(true);
Passing in false
(or no parameter) works - displays all objects.
Is there a way to specify a "zero" value for the first object without resorting to magic number objects (eg having a false entry in the DB just to indicate the N / A value)? Something along the zero-valued lines would be perfect, but I'm lost.
Also, I tried adding new T()
instead null
to the list, but that only resulted in OutOfMemoryException
.
source to share