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
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
Using a data-bind dropdownlist to populate another data-bind dropdownlist mr2_93 Microsoft ASP .NET 1 2nd Oct 2005 06:07 PM
DataGrid Bind to DataSet, then Bind to DaTaview, GOT ERROR...PLS HELP A_PK Microsoft Dot NET Compact Framework 16 13th Apr 2005 04:27 AM
DataGrid Bind to DataSet, then Bind to DaTaview, GOT ERROR...PLS HELP A_PK Microsoft VB .NET 17 13th Apr 2005 04:27 AM
Re: Bind support win2k - Questions about 2000 & Bind Kenneth Porter Microsoft Windows 2000 DNS 9 13th Jul 2003 06:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:47 PM.