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

Discussion in 'ASP .Net' started by Øyvind Isaksen, May 18, 2007.

  1. 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, May 18, 2007
    #1
    1. Advertising

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


    "Øyvind Isaksen" <> wrote in message
    news:...
    >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, May 18, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    3
    Views:
    600
    Natty Gur
    Jul 14, 2003
  2. Luis Esteban Valencia
    Replies:
    1
    Views:
    652
    Joe Fallon
    Jul 3, 2005
  3. mr2_93
    Replies:
    1
    Views:
    3,625
    mr2_93
    Oct 2, 2005
  4. Charlie@CBFC

    Bind DataGrid or DataList to a collection?

    Charlie@CBFC, Dec 20, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,132
    Tod Birdsall, MCSD for .NET
    Dec 20, 2005
  5. Replies:
    0
    Views:
    598
Loading...

Share This Page