Using IEnumerable<> as DataSource

J

Jakub Èermák

Hello *,
I'm still making my first steps in asp.net so please forgive me, if my
problem is trivial.
I've GridView and I wanna to use an IEnumerable from LINQ as its datasource.
When I do
GrivView1.DataSource=from i in xdoc.Root.Element("channel").Elements("item")
select new {
Url = i.Element("link").Value,
Description = i.Element("description").Value,
Title = i.Element("title").Value,
PubTime = DateTime.Parse(i.Element("pubDate").Value)
};

everything works as expected. But when I replace the anonymous type with
normal class it stops working: "The data source for GridView with id
'GridView1' did not have any properties or attributes from which to generate
columns. Ensure that your data source has content. "
Even when I turn off the autogeneratecolumns and make the colums by hand, it
can't find them.
The source code:
GrivView1.DataSource=from i in xdoc.Root.Element("channel").Elements("item")
select new RSSRecord()
{
Url = i.Element("link").Value,
Description = i.Element("description").Value,
title = i.Element("title").Value,
PubTime = DateTime.Parse(i.Element("pubDate").Value)
};

and the RSSRecord class:
public class RSSRecord
{
public string Url,title,Description;
public DateTime PubTime;
}

What can be wrong in the 2nd code, when the 1st code works well? I don't get
the difference.
Thanks in advance for your help.

Jakub Èermák

P.S.: It's just testing/learning code, so please don't comment the variable
names and code structure :)
 
M

Martin Honnen

Jakub said:
and the RSSRecord class:
public class RSSRecord
{
public string Url,title,Description;
public DateTime PubTime;
}

What can be wrong in the 2nd code, when the 1st code works well?

Use properties e.g.
public string Url { get; set; }
public string title { get; set; }
and so on.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top