Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Collection problems (create Collection object, add data to collection, bind collection to datagrid)

Reply
Thread Tools

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

 
 
Řyvind Isaksen
Guest
Posts: n/a
 
      05-18-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
 
      05-18-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

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
this.Controls.Add(Control) Does Not Add Control to child collection (this.Controls.Count == 0) Chad Scharf ASP .Net 3 10-18-2007 03:53 PM
confused by boost::bind & boost::lambda::bind XHengDF@gmail.com C++ 0 05-29-2007 04:37 AM
Using a data-bind dropdownlist to populate another data-bind dropdownlist mr2_93 ASP .Net 1 10-02-2005 05:07 PM
Bind a string collection to a data grid Pete Nelson ASP .Net 2 10-27-2003 09:49 PM
Bind different rows to different data sources in data grid AD ASP .Net 1 08-23-2003 10:34 PM



Advertisments