Wednesday, March 28, 2012

trouble binding a datagrid to a custom collection

I'm trying to bind a custom collection to an autogenerated datagrid without
success.
I'm getting this error:
DataGrid with id 'DataGrid2' could not automatically generate any columns
from the selected data source.
Could you help me to find a solution? What is the problem in my code? Thanks
.
Here is my code (NewsContent.aspx):
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid2" runat="server"></asp:DataGrid>
</form>
//----
Code behind:(NewsContent.aspx.cs).
public class NewsContent{
private void Page_Load(object sender, System.EventArgs e)
{
ListItemCollection myListCollection = AddToCustomCollection();
this.DataGrid2.DataSource = myListCollection;
this.DataGrid2.DataBind();
}
private ListItemCollection AddToCustomCollection()
{
ListItemCollection myListCollection = new ListItemCollection();
NewsContent.NewsItem myItem1 = new NewsContent.NewsItem("viko", "url1",
"today");
NewsContent.NewsItem myItem2 = new NewsContent.NewsItem("vikan", "url2",
"imorgon");
NewsContent.NewsItem myItem3 = new NewsContent.NewsItem("kiki", "url3",
"veckan");
myListCollection.Add(myItem1);
myListCollection.Add(myItem2);
myListCollection.Add(myItem3);
return myListCollection;
}
//Inner Class which holds all properties needed to show news content and
listings
public class NewsItem
{
public NewsItem()
{
}
public NewsItem(string linkName, string linkUrl, string date)
{
}
public string linkName;
public string linkUrl;
public string date;
public string longDate;
}
}
//----
--
I'm using a collection class: (ListItemCollection.cs):
public class ListItemCollection:CollectionBase
{
public ListItemCollection()
{
//
// TODO: Add constructor logic here
//
}
public NewsContent.NewsItem Item(int index)
{
//the appropiate item is retrieved from the list object and
//explicitly cast to the ListItem type, then returned to the
//caller
return (NewsContent.NewsItem) List[index];
}
public int Add(NewsContent.NewsItem myNewsItem)
{
return List.Add(myNewsItem);
}
}Try setting the autogeneratecolumn property for this datagrid to True.
"viktor9990" wrote:

> I'm trying to bind a custom collection to an autogenerated datagrid withou
t
> success.
> I'm getting this error:
> DataGrid with id 'DataGrid2' could not automatically generate any columns
> from the selected data source.
> Could you help me to find a solution? What is the problem in my code? Than
ks.
> Here is my code (NewsContent.aspx):
> <form id="Form1" method="post" runat="server">
> <asp:DataGrid id="DataGrid2" runat="server"></asp:DataGrid>
> </form>
> //----
> Code behind:(NewsContent.aspx.cs).
> public class NewsContent{
> private void Page_Load(object sender, System.EventArgs e)
> {
> ListItemCollection myListCollection = AddToCustomCollection();
> this.DataGrid2.DataSource = myListCollection;
> this.DataGrid2.DataBind();
> }
> private ListItemCollection AddToCustomCollection()
> {
> ListItemCollection myListCollection = new ListItemCollection();
> NewsContent.NewsItem myItem1 = new NewsContent.NewsItem("viko", "url1",
> "today");
> NewsContent.NewsItem myItem2 = new NewsContent.NewsItem("vikan", "url2"
,
> "imorgon");
> NewsContent.NewsItem myItem3 = new NewsContent.NewsItem("kiki", "url3",
> "veckan");
> myListCollection.Add(myItem1);
> myListCollection.Add(myItem2);
> myListCollection.Add(myItem3);
> return myListCollection;
> }
> //Inner Class which holds all properties needed to show news content and
> listings
> public class NewsItem
> {
> public NewsItem()
> {
> }
> public NewsItem(string linkName, string linkUrl, string date)
> {
> }
> public string linkName;
> public string linkUrl;
> public string date;
> public string longDate;
> }
> }
> //---
--
>
> I'm using a collection class: (ListItemCollection.cs):
> public class ListItemCollection:CollectionBase
> {
> public ListItemCollection()
> {
> //
> // TODO: Add constructor logic here
> //
> }
>
> public NewsContent.NewsItem Item(int index)
> {
> //the appropiate item is retrieved from the list object and
> //explicitly cast to the ListItem type, then returned to the
> //caller
> return (NewsContent.NewsItem) List[index];
> }
>
> public int Add(NewsContent.NewsItem myNewsItem)
> {
> return List.Add(myNewsItem);
> }
> }

0 comments:

Post a Comment