Click here to Skip to main content
15,909,498 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello I have a class named AddToGrid and a generic list.
AddToGrid class has properties such as ID, PART, QTY and WEIGHT.

While adding new items to the list from a gridview I want to check if the list has a same item with the same ID or not. if it doesnt have a matching ID I will add it to the list.
The code that I wrote;

for (int i = 0; i < gvParts.Rows.Count; i++)//loop the GridView Rows
        {
            AddToGrid mainList = new AddToGrid();
            mainList.ID = Convert.ToInt32(gvParts.Rows[i].Cells[1].Text);
            mainList.PART = gvParts.Rows[i].Cells[2].Text;
            mainList.QTY = Convert.ToInt32(gvParts.Rows[i].Cells[5].Text);
            string weightlbs = gvParts.Rows[i].Cells[3].Text;
            string[] weight = weightlbs.Split(' ');
            mainList.WEIGHT = Convert.ToDecimal(weight[0]);
            if (rmaPartLstDisp.Count == 0)
            {
                rmaPartLstDisp.Add(mainList);
            }
            else
            {                
                for (int j = 0; j < rmaPartLstDisp.Count; j++)
                {
                    if (!rmaPartLstDisp.Contains(mainList))
                    {
                        rmaPartLstDisp.Add(mainList);                        
                    }
                }
            }
      )      


Inside the for loop on the if statement I need to have something like;

C#
for (int j = 0; j < rmaPartLstDisp.Count; j++)
               {
                   if (!rmaPartLstDisp.Contains(mainList.ID))
                   {
                       rmaPartLstDisp.Add(mainList);
                   }
               }

But it gives an error because of the invalid arguments.
Can someone help me find a proper way to check this? Thanks.
Posted
Comments
Orcun Iyigun 9-Feb-11 15:05pm    
Thanks to all of you who answered my question and the suggestions that you gave.

Consider this part of your code:
C#
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:
C#
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]
C#
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]
 
Share this answer
 
v2
Comments
Orcun Iyigun 9-Feb-11 13:28pm    
Well I actually tried it and didnt get an error but thats not the case though, the main reason is I want to check it with ID is; later I am going to change the quantity so then it will not contain the same values so it will add it thats what i want to prevent.
Henry Minute 9-Feb-11 13:30pm    
OK.
I understand a little better, what you are trying to do. Your original code was nearly right. Give me a moment and I'll edit my answer to suit.
Henry Minute 9-Feb-11 13:38pm    
I have modified my answer to include suitable code.

Hope this helps. :)

Sorry for the misunderstanding.
Orcun Iyigun 9-Feb-11 15:02pm    
Thanks. Yes that did help :)
You don't show the class AddToGrid, that's it. Most likely, your problem is resolved by type case, but the approach is wrong. Anyway, bad class, bad class name (should be noun, not a verb, there are good Microsoft naming conventions). Your problem is lack of generics.

You may need a generic class with item class being a generic parameter. List data can be presented as System.Collections.Generic interfaces and classes.

Also, Contains as a list method if slow operation of there are many items. For fast operation SystemCollections.Generic.Dictionary can be used.

—SA
 
Share this answer
 
1. you could use a dictionary
Dictionary<int, AddToGrid> mycontainer = new Dictionary<int, AddToGrid>();

if(!mycontainer.ContainsKey(myid))
//add it


otherwise the contains is a ref based search you can either create a icomparere or do something like
List<AddToGrid> mylist = new List<AddToGrid>();
if(mylist.FirstOrDefault<AddToGrid>( (item)=>item.Id==mynewId ) !=null)
    //add me
 
Share this answer
 
Well let us see

C#
for (int j = 0; j < rmaPartLstDisp.Count; j++)
{
  if (!rmaPartLstDisp.Contains(mainList))
  {
     rmaPartLstDisp.Add(mainList);
  }
}



it appears that rmaPartLstDisp is a collection of AddToGrid . You need to implement your own Compare method in order for this to work. It is not that hard to do, see here[^] and here[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900