WPF 应用程序 - 无法显示数据网格中列表中的数据
我是 WPF 和 c# 的新手,有一个(可能非常简单)问题。
我正在编写一个小型应用程序,并且尝试从数据库读取数据,将其格式化为列表,然后显示在数据网格中。我已经调试了它,并且我已经成功读取数据库,并且所有数据都在列表中(cd_list
),但它没有将数据传递到数据网格。
下面是我的 XAML:
<DataGrid Name="DataGrid" AutoGenerateColumns="false" RowHeaderWidth="0" Width="240" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" ></DataGridTextColumn>
<DataGridTextColumn Header="Details"></DataGridTextColumn>
<DataGridTextColumn Header="Employee"></DataGridTextColumn>
<DataGridTextColumn Header="Date"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
我的代码隐藏文件执行 InitializeComponent
,然后执行一些工作以从数据库获取数据,并将其放入 call_detail< 列表中/code> 对象:
public class call_details
{
public string name;
public string details;
public string employee;
public string date;
public call_details()
{
}
}
声明列表和对象调用详细对象
public call_details cd_rec = new call_details();
public List<call_details> cd_list = new List<call_details>();
将记录添加到列表
cd_list.Add(cd_rec);
完成此操作后,我尝试提供 ItemsSource 以便数据将显示在数据网格中,并具有以下内容:
DataGrid.ItemsSource = cd_list;
但它不起作用。在 GUI 中,数据网格显示一个网格,其中包含我期望的正确记录数,但它们都是空白的。所以我猜它正在传递一些信息,而不是实际数据。 由于我是新手,这可能是一个愚蠢的错误,但我找不到其他任何东西可以帮助我。
有人吗?
I'm a newbie with WPF and c# and have a (probably very simple) problem.
I'm coding a small application, and i'm trying to read data from a database, format it into a list, and display in a datagrid. Ive debugged it, and it I've successfully read the database, and all the data is in the list (cd_list
), but it's not passing the data to the datagrid.
Below is my XAML:
<DataGrid Name="DataGrid" AutoGenerateColumns="false" RowHeaderWidth="0" Width="240" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" ></DataGridTextColumn>
<DataGridTextColumn Header="Details"></DataGridTextColumn>
<DataGridTextColumn Header="Employee"></DataGridTextColumn>
<DataGridTextColumn Header="Date"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
My code behind file does InitializeComponent
, then does some work to get the data from a DB, and put it in a list of call_detail
objects:
public class call_details
{
public string name;
public string details;
public string employee;
public string date;
public call_details()
{
}
}
Declare list & call detail objects
public call_details cd_rec = new call_details();
public List<call_details> cd_list = new List<call_details>();
Add records to the list
cd_list.Add(cd_rec);
After this is done, I am trying to give the ItemsSource so that the data will show in the Datagrid, and have the following:
DataGrid.ItemsSource = cd_list;
but its not working. In the GUI, the Datagrid shows a grid, with the correct number of records that I expect, but they are all blank.So I guess that it is passing some info, just not the actual data.
It's probably a silly mistake which I have made due to being a novice, but I can't find anything else out there to help me.
Anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所有这些字段都需要作为公共属性公开,并且至少有一个 getter(这是绑定系统的要求)。然后您需要绑定到属性或仅使用自动生成的列。
如果您是 WPF 新手,我强烈建议您阅读一些 MSDN 上的文章。例如数据绑定概述。
All those fields need to be exposed as public properties with at least a getter (this is a requirement of the binding-system). Then you need to bind to the properties or just use the auto-generated columns.
If you are new to WPF i highly recommend reading some of the articles on MSDN. e.g. the Data Binding Overview.