473,587 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.as px):
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid2" runat="server"> </asp:DataGrid>
</form>

//--------------------------------------------------------------------

Code behind:(NewsCon tent.aspx.cs).
public class NewsContent{

private void Page_Load(objec t sender, System.EventArg s e)
{

ListItemCollect ion myListCollectio n = AddToCustomColl ection();
this.DataGrid2. DataSource = myListCollectio n;
this.DataGrid2. DataBind();
}

private ListItemCollect ion AddToCustomColl ection()
{

ListItemCollect ion myListCollectio n = new ListItemCollect ion();
NewsContent.New sItem myItem1 = new NewsContent.New sItem("viko", "url1",
"today");
NewsContent.New sItem myItem2 = new NewsContent.New sItem("vikan", "url2",
"imorgon");
NewsContent.New sItem myItem3 = new NewsContent.New sItem("kiki", "url3",
"veckan");
myListCollectio n.Add(myItem1);
myListCollectio n.Add(myItem2);
myListCollectio n.Add(myItem3);

return myListCollectio n;
}

//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: (ListItemCollec tion.cs):

public class ListItemCollect ion:CollectionB ase
{
public ListItemCollect ion()
{
//
// TODO: Add constructor logic here
//
}
public NewsContent.New sItem 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.Ne wsItem) List[index];
}
public int Add(NewsContent .NewsItem myNewsItem)
{
return List.Add(myNews Item);
}

}
Mar 16 '06 #1
1 2697
Try setting the autogeneratecol umn property for this datagrid to True.
"viktor9990 " wrote:
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.as px):
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid2" runat="server"> </asp:DataGrid>
</form>

//--------------------------------------------------------------------

Code behind:(NewsCon tent.aspx.cs).
public class NewsContent{

private void Page_Load(objec t sender, System.EventArg s e)
{

ListItemCollect ion myListCollectio n = AddToCustomColl ection();
this.DataGrid2. DataSource = myListCollectio n;
this.DataGrid2. DataBind();
}

private ListItemCollect ion AddToCustomColl ection()
{

ListItemCollect ion myListCollectio n = new ListItemCollect ion();
NewsContent.New sItem myItem1 = new NewsContent.New sItem("viko", "url1",
"today");
NewsContent.New sItem myItem2 = new NewsContent.New sItem("vikan", "url2",
"imorgon");
NewsContent.New sItem myItem3 = new NewsContent.New sItem("kiki", "url3",
"veckan");
myListCollectio n.Add(myItem1);
myListCollectio n.Add(myItem2);
myListCollectio n.Add(myItem3);

return myListCollectio n;
}

//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: (ListItemCollec tion.cs):

public class ListItemCollect ion:CollectionB ase
{
public ListItemCollect ion()
{
//
// TODO: Add constructor logic here
//
}
public NewsContent.New sItem 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.Ne wsItem) List[index];
}
public int Add(NewsContent .NewsItem myNewsItem)
{
return List.Add(myNews Item);
}

}

Mar 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2333
by: Ann Morris | last post by:
INTRODUCTION One of the most powerful aspects of .NET and Windows Forms is data binding. Data binding is the process of associating user interface (UI) elements with a data source to generate a visual representation of data. Two types of data binding are available for Windows Forms: Simple Data Binding and Complex Data Binding. Simple...
5
2524
by: K | last post by:
I created a collection which is derived from ArrayList and implements IBindingList and ITypedList. Then I bound the DataSource of a data grid into the collection. It could show up the data but the column heading is not in the right order, for eg. I want "ID" column to be the first column but it showed as the second column. Another problem...
1
2649
by: Joe Jax | last post by:
I'm building my own version of a readonly DataTable, which needs to bind to various controls. My data table class has two main collections (CollectionBase instances): one of columns, one of rows, where each row has a collection of column values. I have implemented the ITypedList interface on my rows collection, but when I bind the collection...
3
1652
by: Just D | last post by:
Hi, If anybody needs to show some data retrieved from the database table, what method is more preferrable? 1. DataSet ds = ...; DataGrid.Data.Source.ds; DataGrid.Bind(); 2. Write a custom object, fill it with the DataSet data and ÅÒÕÔ try to bind
1
3152
by: Demetri | last post by:
Someone posted the following back in June and I am now doing the same thing. I'll just paste what was asked and see if anyone can give us an answer: --------------------------------------------------------------------------------- I have a custom collection of objects, each of which includes a child object called MyUserOpener. In declarative...
16
1344
by: scorpion53061 | last post by:
Well as some of you know I was using a tab control for a project I was building for my boss. Today he tells me that he wants: When he switches tabs to be able to switch back and see whatever it was he was doing on the other tab. That may not sound like a big thing but I used the same controls for each tab and then manually cleared the...
0
1286
by: Stephajn Craig | last post by:
I've built a customized collection class that has strongly typed objects in it. I have a collection class called LogEntryCollection with LogEntry objects in it. Each LogEntry object has certain properties similar to the one below: Public Property LogDate() As Date Get Return _LogDate End Get
2
2210
by: Merk | last post by:
I'm new to .NET (using 2.0) and was wondering how to go about binding a collection of custom classes to a DataGrid. The class exposes 5 read-only String properties. I would like for each of these properties to appear in a column in the DataGrid. The end result would be that when the collection has 35 objects in it, the DataGrid would have...
1
1917
by: Suresh | last post by:
Using ASP.NET 1.1 ---------------------- I have a custom collection of object that I'm binding to a datagrid. Each of these objects have another collection inside them. I have a user control that renders the nested collection into html table. See sample code below. Now this binds fine and my sub collections are rendered fine by my user...
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.