WPF 应用程序 - 无法显示数据网格中列表中的数据

发布于 2024-11-07 13:18:50 字数 1583 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2024-11-14 13:18:50

所有这些字段都需要作为公共属性公开,并且至少有一个 getter(这是绑定系统的要求)。然后您需要绑定到属性或仅使用自动生成的列。

public string Name { get; private set; }
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />

如果您是 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.

public string Name { get; private set; }
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />

If you are new to WPF i highly recommend reading some of the articles on MSDN. e.g. the Data Binding Overview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文