473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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

May 18 '07 #1
1 1470
I got it, I was missing this:
ArticleAttribute = new Test.ArticleAttribute(); // create a new
object
"Øyvind Isaksen" <ho***@egmont.nowrote in message
news:O2**************@TK2MSFTNGP04.phx.gbl...
>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???

May 18 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: SammyBar | last post by:
Hi, I'm trying to bind a custom collection class to a data grid, following the guidelines from the article http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx....
7
by: Pete Davis | last post by:
A different question this time. I have a DataGrid bound to a collection. Is there any way for me to allow sorting? The DataGrid.AllowSorting=true doesn't work, but that's probably because it can't...
2
by: Pete Nelson | last post by:
Does anyone know of a way to bind a string collection to a datagrid? Basically, I have an object, CreditPlans, and a string collection, ModelNumbers. I'd like to bind the ModelNumbers collection...
8
by: ASP Yaboh | last post by:
I have an ArrayList of data gathered from a database. I want to create a web page from this data by creating a <table>, each cell in each row displays the appropriate data. One of those cells in...
1
by: Luis Esteban Valencia | last post by:
Please everybody participate in this question. Hello my applicacion has many layers and classes and I think its well structured. PLease let me know if you disagree and why. 1. I have the user...
2
by: trebor | last post by:
I bind a collection to a data grid, and it works okay, but when I try to create customized columns, it just doesn't work. Here is the code. Note that if I uncomment that section below, and use "DT"...
2
by: Ivan Weiss | last post by:
Hi. This is a question I have posted before and have never figured out a working result. I have been recommended to read some books which I did which still have not answered my question. I am...
6
by: rodchar | last post by:
hey all, Dim _oItems As Outlook.Items Dim oInbox As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) _oItems = oInbox.Items I can iterate thru this with a...
1
by: Øyvind Isaksen | last post by:
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.