Consider this part of your code:
if (rmaPartLstDisp.Count == 0)
{
rmaPartLstDisp.Add(mainList);
}
else
{
for (int j = 0; j < rmaPartLstDisp.Count; j++)
{
if (!rmaPartLstDisp.Contains(mainList))
{
rmaPartLstDisp.Add(mainList);
}
}
}
First you check if the list count is zero. If it is then it cannot contain your
mainList
instance, so you add it. If the count is greater than zero you want to add it if it is not there.
So, regardless of the number of items in
rmaPartLstDisp
you want to add the
mainList
instance if it isn't there. Why bother doing all that checking then?
Simply replace all of that with:
if (!rmaPartLstDisp.Contains(mainList))
{
rmaPartLstDisp.Add(mainList);
}
If that was throwing an error before, it probably still will. So modify your question to add the new code and copy the
exact error message.
[Edit added after OPs comment]
if (rmaPartLstDisp.Count == 0)
{
rmaPartLstDisp.Add(mainList);
}
else
{
bool itemFound = false;
foreach (AddToGrid atg in rmaPartLstDisp)
{
if (atg.ID == mainList.ID)
{
itemFound = true;
break;
}
}
if (!itemFound)
{
rmaPartLstDisp.Add(mainList);
}
}
should do what you want.
[/Edit]