PC Review


Reply
Thread Tools Rate Thread

Collection problems (create Collection object, add data to collection, bind collection to datagrid)

 
 
Øyvind Isaksen
Guest
Posts: n/a
 
      18th May 2007
I try to make my own ArticleAttribute object and ArticleAttributeCollection,
and add data to this Collection. It almost works, but the problem is that
each time I add an ArticleAttribute to my Collection, it seems like it
overwrites the other ArticleAttributes. When bind the
ArticleAttributeCollection to a datagrid, all articleattributes are the
same.

This is what my datagrid dispays:

ID Content
2 Hello 2
2 Hello 2
2 Hello 2

It should be like this:

ID Content
0 Hello 0
1 Hello 1
2 Hello 2

-----------------------------------------------
Here is my ArticleAttributeCollection.cs
-----------------------------------------------

namespace Test
{
[Serializable()]
public class ArticleAttributeCollection : CollectionBase, IEnumerable
{

public void Insert(int index, ArticleAttribute ArticleAttribute)
{
base.List.Insert(index, ArticleAttribute);
}

public void Add(ArticleAttribute ArticleAttribute)
{
base.List.Add(ArticleAttribute);
}

public void Remove(ArticleAttribute ArticleAttribute)
{
base.List.Remove(ArticleAttribute);
}

public ArticleAttribute this[int index]
{
get
{
return (ArticleAttribute)(base.List[index]);
}
set
{
base.List[index] = value;
}
}

}
}

-----------------------------------------------
Here is my ArticleAttribute.cs
-----------------------------------------------

namespace Test
{
public class ArticleAttribute
{

public ArticleAttribute()
{
}

public ArticleAttribute(int templateDefinitionId, int articleId,
string content, string templateDefinitionName, string
templateDefinitionHelpText)
{
this._TemplateDefinitionId = templateDefinitionId;
this.ArticleId = articleId;
this.Content = content;
this.TemplateDefinitionName = templateDefinitionName;
this.TemplateDefinitionHelpText = templateDefinitionHelpText;
}

private int _TemplateDefinitionId;
public int TemplateDefinitionId
{
get { return _TemplateDefinitionId; }
set { _TemplateDefinitionId = value; }
}

private int _ArticleId;
public int ArticleId
{
get { return _ArticleId; }
set { _ArticleId = value; }
}

private string _Content;
public string Content
{
get { return _Content; }
set { _Content = value; }
}

private string _TemplateDefinitionName;
public string TemplateDefinitionName
{
get { return _TemplateDefinitionName; }
set { _TemplateDefinitionName = value; }
}

private string _TemplateDefinitionHelpText;
public string TemplateDefinitionHelpText
{
get { return _TemplateDefinitionHelpText; }
set { _TemplateDefinitionHelpText = value; }
}

}
}


-----------------------------------------------
Here I adds articleAttributes to the Collection, and bind it to a datagrid.
It returns 3 items, all with the same ID and Content... Why??
-----------------------------------------------

protected void Page_Load(object sender, EventArgs e)
{
Test.ArticleAttributeCollection ArticleAttributeCollection = new
Test.ArticleAttributeCollection();
Test.ArticleAttribute ArticleAttribute = new Test.ArticleAttribute();

int i = 0;

while (i<2)
{
ArticleAttribute.ArticleId = i;
ArticleAttribute.Content = "Innhold " + i;
ArticleAttributeCollection.Add(ArticleAttribute);
i++;
}

Response.Write(ArticleAttributeCollection.Count);

this.dtgTest.DataSource = ArticleAttributeCollection;
this.dtgTest.DataBind();
}


WHAT IS WRONG IN MY CODE???



 
Reply With Quote
 
 
 
 
Øyvind Isaksen
Guest
Posts: n/a
 
      18th May 2007
I got it, I was missing this:
ArticleAttribute = new Test.ArticleAttribute(); // create a new
object


"Øyvind Isaksen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I try to make my own ArticleAttribute object and
>ArticleAttributeCollection,
> and add data to this Collection. It almost works, but the problem is that
> each time I add an ArticleAttribute to my Collection, it seems like it
> overwrites the other ArticleAttributes. When bind the
> ArticleAttributeCollection to a datagrid, all articleattributes are the
> same.
>
> This is what my datagrid dispays:
>
> ID Content
> 2 Hello 2
> 2 Hello 2
> 2 Hello 2
>
> It should be like this:
>
> ID Content
> 0 Hello 0
> 1 Hello 1
> 2 Hello 2
>
> -----------------------------------------------
> Here is my ArticleAttributeCollection.cs
> -----------------------------------------------
>
> namespace Test
> {
> [Serializable()]
> public class ArticleAttributeCollection : CollectionBase, IEnumerable
> {
>
> public void Insert(int index, ArticleAttribute ArticleAttribute)
> {
> base.List.Insert(index, ArticleAttribute);
> }
>
> public void Add(ArticleAttribute ArticleAttribute)
> {
> base.List.Add(ArticleAttribute);
> }
>
> public void Remove(ArticleAttribute ArticleAttribute)
> {
> base.List.Remove(ArticleAttribute);
> }
>
> public ArticleAttribute this[int index]
> {
> get
> {
> return (ArticleAttribute)(base.List[index]);
> }
> set
> {
> base.List[index] = value;
> }
> }
>
> }
> }
>
> -----------------------------------------------
> Here is my ArticleAttribute.cs
> -----------------------------------------------
>
> namespace Test
> {
> public class ArticleAttribute
> {
>
> public ArticleAttribute()
> {
> }
>
> public ArticleAttribute(int templateDefinitionId, int articleId,
> string content, string templateDefinitionName, string
> templateDefinitionHelpText)
> {
> this._TemplateDefinitionId = templateDefinitionId;
> this.ArticleId = articleId;
> this.Content = content;
> this.TemplateDefinitionName = templateDefinitionName;
> this.TemplateDefinitionHelpText = templateDefinitionHelpText;
> }
>
> private int _TemplateDefinitionId;
> public int TemplateDefinitionId
> {
> get { return _TemplateDefinitionId; }
> set { _TemplateDefinitionId = value; }
> }
>
> private int _ArticleId;
> public int ArticleId
> {
> get { return _ArticleId; }
> set { _ArticleId = value; }
> }
>
> private string _Content;
> public string Content
> {
> get { return _Content; }
> set { _Content = value; }
> }
>
> private string _TemplateDefinitionName;
> public string TemplateDefinitionName
> {
> get { return _TemplateDefinitionName; }
> set { _TemplateDefinitionName = value; }
> }
>
> private string _TemplateDefinitionHelpText;
> public string TemplateDefinitionHelpText
> {
> get { return _TemplateDefinitionHelpText; }
> set { _TemplateDefinitionHelpText = value; }
> }
>
> }
> }
>
>
> -----------------------------------------------
> Here I adds articleAttributes to the Collection, and bind it to a
> datagrid.
> It returns 3 items, all with the same ID and Content... Why??
> -----------------------------------------------
>
> protected void Page_Load(object sender, EventArgs e)
> {
> Test.ArticleAttributeCollection ArticleAttributeCollection = new
> Test.ArticleAttributeCollection();
> Test.ArticleAttribute ArticleAttribute = new Test.ArticleAttribute();
>
> int i = 0;
>
> while (i<2)
> {
> ArticleAttribute.ArticleId = i;
> ArticleAttribute.Content = "Innhold " + i;
> ArticleAttributeCollection.Add(ArticleAttribute);
> i++;
> }
>
> Response.Write(ArticleAttributeCollection.Count);
>
> this.dtgTest.DataSource = ArticleAttributeCollection;
> this.dtgTest.DataBind();
> }
>
>
> WHAT IS WRONG IN MY CODE???
>
>
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding class object to collection repeats same object through collection? Erazmus Microsoft Excel Programming 2 17th Sep 2007 04:35 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft ASP .NET 1 18th May 2007 10:24 AM
Everybody Read this and participate. How to bind custom object collection to a datagrid.? Luis Esteban Valencia Microsoft ASP .NET 1 3rd Jul 2005 05:40 PM
Using collection as data object to a DataGrid David K. Microsoft C# .NET 1 14th Dec 2004 06:02 PM
string collection data to bind to, datagrid already binded to typed dataset usha Microsoft Dot NET Framework Forms 1 14th Jul 2004 12:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:50 PM.