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

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

问题描述



我正在使用WCF应用程序作为我的主机,在这里我保留我的显示数据,添加数据等方法,我有WPF应用程序作为我的客户端,我使用代码来显示从数据库到我的ListView的数据。



这是我的代码

  ServiceReference1.ImojWCFServiceClient client = new ServiceReference1.ImojWCFServiceClient(); 
listView1.Items.Clear();
var userList = client.getUsers();
foreach(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);
}

这是我获得的错误



有人可以帮忙我修改这段代码?



任何帮助都不胜感激!

解决方案

如果您的 getUsers 方法返回列表,数组或类似方法,则可以将ListView的ItemsSource设置为 userList 。例如:

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

如果不起作用,在设置ItemsSource

$之前将userList转换为ObservableCollection b
$ b

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

 < ListView> 
< ListView.View>
< GridView>
< GridViewColumn Header =IDDisplayMemberBinding ={Binding UserID}/>
< GridViewColumn Header =NameDisplayMemberBinding ={Binding Name}/>
< GridViewColumn Header =Last NameDisplayMemberBinding ={Binding LastName}/>
< / GridView>
< /ListView.View>
< / ListView>


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 to userList. For example:

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

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中将数据从数据库显示到ListView C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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