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

Ø

Øyvind Isaksen

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???
 
Ø

Øyvind Isaksen

I got it, I was missing this:
ArticleAttribute = new Test.ArticleAttribute(); // create a new
object


Øyvind Isaksen said:
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???
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top