Can't bind IEnumerable to DataGridView

R

Ronald S. Cook

Does anyone know why an IEnumerable might not bind to a DataGridView?

Thanks,
Ron
 
N

Nicholas Paldino [.NET/C# MVP]

Ron,

Is this in System.Windows.Forms or in ASP.NET? If the answer is the
former, then the requirement is that the collection implement IList, not
IEnumerable. With ASP.NET, I think you ^should^ be able to bind an
IEnumerable to a data source, but you would have to check up on that.
 
R

Ronald S. Cook

Yes, is't a Win Forms app and I can bind other IEnumerables. Just not sure
why not this one. Here it is in case you notice anything:

Public Function GetProgramPriceListByMedicineId(ByVal MedicineId) As
IEnumerable

Dim y As IEnumerable(Of ProgramMedicinePrice)

y = From pmp In dc.ProgramMedicinePrices _
Where pmp.MedicineId = MedicineId

Dim x As IEnumerable
x = From xy In y _
Where xy.ProgramMedicinePriceDate = (From test In y _
Where test.ProgramId =
xy.ProgramId _
Select
test.ProgramMedicinePriceDate).Max _
Select xy.Program.ProgramName, _
xy.ProgramMedicinePriceDate, _
xy.ProgramMedicinePriceDollars

Return x



Nicholas Paldino said:
Ron,

Is this in System.Windows.Forms or in ASP.NET? If the answer is the
former, then the requirement is that the collection implement IList, not
IEnumerable. With ASP.NET, I think you ^should^ be able to bind an
IEnumerable to a data source, but you would have to check up on that.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronald S. Cook said:
Does anyone know why an IEnumerable might not bind to a DataGridView?

Thanks,
Ron
 
N

Nicholas Paldino [.NET/C# MVP]

Ronald,

Just because it does work doesn't mean that it's designed to work that
way. Ultimately, anything that implements IList is bindable, and that's the
minimum requirement for binding to data in Windows Forms (according to the
framework docs).

So, you will have to make sure that whatever IEnumerable implementations
you have are populated into IList implementations before binding.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronald S. Cook said:
Yes, is't a Win Forms app and I can bind other IEnumerables. Just not
sure why not this one. Here it is in case you notice anything:

Public Function GetProgramPriceListByMedicineId(ByVal MedicineId) As
IEnumerable

Dim y As IEnumerable(Of ProgramMedicinePrice)

y = From pmp In dc.ProgramMedicinePrices _
Where pmp.MedicineId = MedicineId

Dim x As IEnumerable
x = From xy In y _
Where xy.ProgramMedicinePriceDate = (From test In y _
Where test.ProgramId =
xy.ProgramId _
Select
test.ProgramMedicinePriceDate).Max _
Select xy.Program.ProgramName, _
xy.ProgramMedicinePriceDate, _
xy.ProgramMedicinePriceDollars

Return x



Nicholas Paldino said:
Ron,

Is this in System.Windows.Forms or in ASP.NET? If the answer is the
former, then the requirement is that the collection implement IList, not
IEnumerable. With ASP.NET, I think you ^should^ be able to bind an
IEnumerable to a data source, but you would have to check up on that.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronald S. Cook said:
Does anyone know why an IEnumerable might not bind to a DataGridView?

Thanks,
Ron
 
R

Ronald S. Cook

I actually figured it out. I needed to encapsulate the second query in
parentheses and have .ToList() at the end. I guess querying an in-memnroy
object vs querying a database data source was the difference.

Thanks for looking at it.


Nicholas Paldino said:
Ronald,

Just because it does work doesn't mean that it's designed to work that
way. Ultimately, anything that implements IList is bindable, and that's
the minimum requirement for binding to data in Windows Forms (according to
the framework docs).

So, you will have to make sure that whatever IEnumerable
implementations you have are populated into IList implementations before
binding.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronald S. Cook said:
Yes, is't a Win Forms app and I can bind other IEnumerables. Just not
sure why not this one. Here it is in case you notice anything:

Public Function GetProgramPriceListByMedicineId(ByVal MedicineId) As
IEnumerable

Dim y As IEnumerable(Of ProgramMedicinePrice)

y = From pmp In dc.ProgramMedicinePrices _
Where pmp.MedicineId = MedicineId

Dim x As IEnumerable
x = From xy In y _
Where xy.ProgramMedicinePriceDate = (From test In y _
Where test.ProgramId
= xy.ProgramId _
Select
test.ProgramMedicinePriceDate).Max _
Select xy.Program.ProgramName, _
xy.ProgramMedicinePriceDate, _
xy.ProgramMedicinePriceDollars

Return x



Nicholas Paldino said:
Ron,

Is this in System.Windows.Forms or in ASP.NET? If the answer is the
former, then the requirement is that the collection implement IList, not
IEnumerable. With ASP.NET, I think you ^should^ be able to bind an
IEnumerable to a data source, but you would have to check up on that.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Does anyone know why an IEnumerable might not bind to a DataGridView?

Thanks,
Ron
 
N

Nicholas Paldino [.NET/C# MVP]

Ronald,

Well, this is why. ToList returns a List<T>, which implements IList =)

When querying against an IQueryable implementation, it might be that it
returns an implementation that returns an IQueryable implementation, which
^also^ implements IList, but that's an implementation detail, at least from
what I can tell, and should not be relied upon.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronald S. Cook said:
I actually figured it out. I needed to encapsulate the second query in
parentheses and have .ToList() at the end. I guess querying an in-memnroy
object vs querying a database data source was the difference.

Thanks for looking at it.


Nicholas Paldino said:
Ronald,

Just because it does work doesn't mean that it's designed to work that
way. Ultimately, anything that implements IList is bindable, and that's
the minimum requirement for binding to data in Windows Forms (according
to the framework docs).

So, you will have to make sure that whatever IEnumerable
implementations you have are populated into IList implementations before
binding.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ronald S. Cook said:
Yes, is't a Win Forms app and I can bind other IEnumerables. Just not
sure why not this one. Here it is in case you notice anything:

Public Function GetProgramPriceListByMedicineId(ByVal MedicineId) As
IEnumerable

Dim y As IEnumerable(Of ProgramMedicinePrice)

y = From pmp In dc.ProgramMedicinePrices _
Where pmp.MedicineId = MedicineId

Dim x As IEnumerable
x = From xy In y _
Where xy.ProgramMedicinePriceDate = (From test In y _
Where test.ProgramId
= xy.ProgramId _
Select
test.ProgramMedicinePriceDate).Max _
Select xy.Program.ProgramName, _
xy.ProgramMedicinePriceDate, _
xy.ProgramMedicinePriceDollars

Return x



in message Ron,

Is this in System.Windows.Forms or in ASP.NET? If the answer is the
former, then the requirement is that the collection implement IList,
not IEnumerable. With ASP.NET, I think you ^should^ be able to bind an
IEnumerable to a data source, but you would have to check up on that.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Does anyone know why an IEnumerable might not bind to a DataGridView?

Thanks,
Ron
 
M

Marc Gravell

    When querying against an IQueryable implementation, it might be that it
returns an implementation that returns an IQueryable implementation, which
^also^ implements IList

I think it is more likely that the IQueryable implementation
implements IListSource, and the IListSource.GetList() implementation
essentially does a ToList(); but this is (as Nicholas mentions) an
implementation detail an cannot be generalised - for example the
following IQueryable<T> does not behave this way (it should not be
assumed); add a ToList after (or instead of) the AsQueryable() and it
will work.

In short: grids require a list; be sure and give it such...

class Foo {
public int Bar {get;set;}
}
static IEnumerable<Foo> Data{get{
yield return new Foo { Bar = 1 };
yield return new Foo { Bar = 3 };
yield return new Foo { Bar = 5 };
}}
static void Main() {
var query = Data.AsQueryable(); // .ToList();
using (Form form = new Form {
Controls = {new DataGridView{DataSource = query}}
}) {
Application.Run(form);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top