如何显示在WPF C#从数据库中数据的ListView [英] How to display data from database to ListView in WPF C#

查看:757
本文介绍了如何显示在WPF C#从数据库中数据的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Entitity框架数据库的数据显示,以我在我的WPF C#应用程序的ListView。

I'm trying to display data from my Entitity Framework database to my ListView in my WPF C# application.

我使用WCF应用程序作为我的主机,在这里我把我的方法来显示数据,添加数据,等等,我有WPF应用程序为我的客户,我在那里用code显示从数据库中的数据我的ListView。

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.

这是我的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

有人可以帮我解决这个问题code?

Can somebody help me fix this code?

任何帮助是AP preciated!

推荐答案

如果你的 getUsers 方法返回一个列表,数组或相似的,你可以简单地设置的的ItemsSource在ListView到用户列表。例如:

If your getUsers method returns a list, array, or similar, you could simply set the ItemsSource of the ListView to userList. For example:

ServiceReference1.ImojWCFServiceClient client = new ServiceReference1.ImojWCFServiceClient();
listView1.Items.Clear();
var userList = client.getUsers();
listView1.ItemsSource = userList;

如果不工作,转换到用户列表一个ObservableCollection设置的ItemsSource之前

If that doesn't work, convert userList to an ObservableCollection before setting the ItemsSource

您的XAML可能看起来像这样...

Your Xaml might look like this...

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="ID" DisplayMemberBinding="{Binding UserID}"/>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}"/>
        </GridView>
    </ListView.View>
</ListView>

这篇关于如何显示在WPF C#从数据库中数据的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆