I'm trying to display data from my Entitity Framework database to my ListView in my WPF C# application.
I'm using WCF app as my host, where I keep my methods for displaying data, adding data, etc., and I have WPF app as my client, where I use the code to display data from database to my ListView.
This is my code
ServiceReference1.ImojWCFServiceClient client = new ServiceReference1.ImojWCFServiceClient();
listView1.Items.Clear();
var userList = client.getUsers();
foreach (var user in userList)
{
ListViewItem listOfUsers;
string[] list = new string[3];
list[0] = user.UserID.ToString();
list[1] = user.Name;
list[2] = user.LastName;
listOfUsers = new ListViewItem(list);
listView1.Items.Add(listOfUsers);
}
This is the error that I get
Can somebody help me fix this code?
Any help is appreciated!
If your
getUsers
method returns a list, array, or similar, you could simply set the ItemsSource of the ListView touserList
. For example:If that doesn't work, convert userList to an ObservableCollection before setting the ItemsSource
Your Xaml might look like this...