Click here to Skip to main content
15,306,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 populated ListView controls:

ListView1 (columns: Reference, State, Description) ListView2 (columns: Reference, State, Description)

I need to try and loop through the rows\items in ListView1, and using the Reference Column as a key field, identify whether there are differences in the columns State and Description between the 2 ListView datasets..

The data in the 2 listview controls will not be in the same order and ListView2 may contain additional rows not present in ListView1

Essentially I need to loop through the Reference fields in ListView1 - find the corresponding\matching entry in Listview2 and compare the State and Description fields across the 2 ListView controls..

im afraid at this stage I dont have any example code as ive not being able to get close to working this out.


What I have tried:

im afraid at this stage I dont have any example code as ive not being able to get close to working this out.
Posted
Updated 20-Jul-21 22:41pm
Comments
[no name] 20-Jul-21 11:16am
   
This sounds a lot like a homework assignment. The steps that you need to accomplish this have been stated. First, you will have to sort both lists to match. Loop through the lists using nested loops. Since the Listview control types haven't been mentioned (WinForms, WPF), I'm assuming Winforms. The ListView controls take the data in as character separated data for the columns and the column order is consistent between lists. You will need to split the list values into their respective columns between both lists and compare.
Gerry Schmitz 20-Jul-21 11:48am
   
Compare the data (sources); not the "UI".
Chris Smith Jul2021 20-Jul-21 13:23pm
   
Ah and this is where I'm getting stuck..I cant order the 2 sets of items because the lists contain different values..

So list 1 may have
1,aaa,Bbb
2,ccc,ddd
3,eee,fff

Where as list 2 would have
2,ccc,ddd
8,xxx,ZZZ

(The only common data in the example above is number 2)

So if I order them there not in sync and don't match as list 2 will have values not present in list 1..

This is what I'm struggling with..
In the example above I need to loop through list 1, check if the data row exists in list 2 (compare by the first data field/value - so the '2' in this case) and if so compare the other data fields in that row..

Ha its definitely not a homework assignment, not at my age, but I am just starting out on my dev journey..

Happy just to get some pointers as to how do this..

1 solution

Quote:
Essentially I need to loop through the Reference fields in ListView1 - find the corresponding\matching entry in Listview2 and compare the State and Description fields across the 2 ListView controls..


You're wrong! You have to create a list of custom class and implement Equals method. How? See: How to define value equality for a class or struct - C# Programming Guide | Microsoft Docs[^]

Here's an idea:
C#
void Main()
{
	List<MyLvObject> l1 = new List<MyLvObject>()
		{
			new MyLvObject(){Reference = 1, State="aaa", Description="Bbb"},
			new MyLvObject(){Reference = 2, State="ccc", Description="ddd"},
			new MyLvObject(){Reference = 3, State="eee", Description="fff"},
		};
		
	List<MyLvObject> l2 = new List<MyLvObject>()
		{
			new MyLvObject(){Reference = 2, State="ccc", Description="ddd"},
			new MyLvObject(){Reference = 8, State="xxx", Description="z"},
		};

	foreach(MyLvObject m1 in l1)
	{
		Console.WriteLine(m1.ToString());
		foreach(MyLvObject m2 in l2)
		{
			Console.WriteLine($"\t{m2.ToString()} => {m1.Equals(m2)}");
		}
	}
}

// Define other methods and classes here
public class MyLvObject: IEquatable<MyLvObject>
{
	public int Reference {get; set;} = 0;
	public string State {get; set;} = string.Empty;
	public string Description {get; set;} = string.Empty;
	
	public override int GetHashCode() => (State, Description).GetHashCode();
	
	public bool Equals(MyLvObject mlo)
	{
        if (mlo is null || !this.GetType().Equals(mlo.GetType()))
        {
            return false;
        }
		else
		{
			return State.Equals(mlo.State) && Description.Equals(mlo.Description);
		}
	}
	
	public override string ToString()
	{
		return $"{Reference} {State} {Description}";
	}
}


Result:
1 aaa Bbb
  2 ccc ddd => False
  8 xxx z => False
2 ccc ddd
  2 ccc ddd => True
  8 xxx z => False
3 eee fff
  2 ccc ddd => False
  8 xxx z => False


Note: you can use overloaded method of String.Equals with InvariantCultureIgnoreCase parameter to ignore upper/lower case. See: String.Equals Method (System) | Microsoft Docs[^]
   
v2
Comments
TheRealSteveJudge 21-Jul-21 4:52am
   
Good approach! 5*
Maciej Los 21-Jul-21 4:55am
   
Thank you.
:)

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