wpf 如何在WPF C#中将数据从数据库显示到ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27217727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:45:21  来源:igfitidea点击:

How to display data from database to ListView in WPF C#

c#wpfentity-frameworklistviewservice-reference

提问by aiden87

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

我正在尝试将数据从我的实体框架数据库显示到我的 WPF C# 应用程序中的 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.

我使用 WCF 应用程序作为我的主机,在那里我保留了显示数据、添加数据等的方法,并且我将 WPF 应用程序作为我的客户端,在那里我使用代码将数据从数据库显示到我的 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 getenter image description here

这是我得到的错误在此处输入图片说明

Can somebody help me fix this code?

有人可以帮我修复此代码吗?

Any help is appreciated!

任何帮助表示赞赏!

回答by learningcs

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

如果您的getUsers方法返回一个列表、数组或类似的东西,您可以简单地将 ListView 的 ItemsSource 设置为userList。例如:

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

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

如果这不起作用,请在设置 ItemsSource 之前将 userList 转换为 ObservableCollection

Your Xaml might look like this...

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

<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>

回答by Robert Harvey

ListViewItemdoesn't have a constructor that takes one argument. So listOfUsers = new ListViewItem(list);isn't going to work.

ListViewItem没有接受一个参数的构造函数。所以listOfUsers = new ListViewItem(list);不会工作。

Try an approach like this:

尝试的方法是这样

var items = new List<User>();
items.Add(new User() { Name = "John Doe", Age = 42 });
items.Add(new User() { Name = "Jane Doe", Age = 39 });
items.Add(new User() { Name = "Sammy Doe", Age = 13 });
myListView.ItemsSource = items;