Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have the following codes in which I will have a loop. For example, I have 2 different records, let say "Hello" and "HelloWorld" inside my DtoList. But somehow the "Hello" and "HelloWorld" will duplicate themselves and have 2 records for each off them.
Which means total of 4 records inside my dtolist.
Therefore, everytime the "tvQuestionList" is refreshed, it will displays total of 4 records instead of 2 records.
How can I change the codes logic below to make it become only 2 records to be displayed?
Below are my codes:

C#
private void PopulateQuestionTree(bool isDelete)
{
    tvQuestionList.BindingContext = null;
    tvQuestionList.Nodes.Clear();
    CustomDataCollectionQuestionDtoList questionList = new CustomDataCollectionQuestionDtoList();
    questionList.DtoList = _customDataCollectionQuestionList.DtoList.Where(q =>
        q.TrackingState != BaseDto.TrackingInfo.Deleted).OrderBy(q => q.Sequence).ToList();
    for (int i = 0; i < questionList.DtoList.Count; i++)
    {  
         tvQuestionList.Nodes.Insert(i, CreateTreeNode(questionList.DtoList[i], i));       
    }
    tvQuestionList.Refresh();
}
Posted
Comments
Jamie888 19-May-15 4:58am    
FYI, I have checked through the SQL database. The records indeed are correct. Only one "Hello" and one "HelloWorld".
Mycroft Holmes 19-May-15 5:04am    
Have you checked what the count of DtoList is when you start the loop.
Jamie888 19-May-15 5:09am    
yup, it is 4. I feel weird though. Is it because of the "questionList.DtoList = _customDataCollectionQuestionList.DtoList.Where(q =>
q.TrackingState != BaseDto.TrackingInfo.Deleted).OrderBy(q => q.Sequence).ToList();" ?

Jamie888 19-May-15 5:14am    
Sir, FYI, the sequence for the results are "Hello", "Hello", "HelloWorld", "HelloWorld". Could I add a if(i%2 == 0){tvQuestionList.Nodes.Insert(i, CreateTreeNode(questionList.DtoList[i], i));} ?
Matt T Heffron 19-May-15 12:49pm    
I'd unroll the filling of questionList.DtoList so I could step through it with the debugger to see what is really happening.

1 solution

C#
private void PopulateQuestionTree(bool isDelete)
{
    tvQuestionList.BindingContext = null;
    tvQuestionList.Nodes.Clear();
    CustomDataCollectionQuestionDtoList questionList = new CustomDataCollectionQuestionDtoList();
    questionList.DtoList = _customDataCollectionQuestionList.DtoList.Where(q =>
        q.TrackingState != BaseDto.TrackingInfo.Deleted).OrderBy(q => q.Sequence).ToList();
    for (int i = 0; i < questionList.DtoList.Count; i++)
    {
           if(tvQuestionList.Nodes.Contains(x=>x.Name!=questionList.DtoList[i].Name)
         tvQuestionList.Nodes.Insert(i, CreateTreeNode(questionList.DtoList[i], i));
    }
    tvQuestionList.Refresh();
}
 
Share this answer
 
v2
Comments
Matt T Heffron 19-May-15 12:48pm    
+4This addresses the symptom, not the root cause.
(Why does it appear the records are duplicated?)
Jamie888 21-May-15 23:39pm    
Sir, I have tried your method but it return an error message "Cannot convert lambda expression to type "System.Windows.Forms.TreeNode" because it is not a delegate type".

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