【问题标题】:WPF DataGrid Is Not Filled With DataWPF DataGrid 未填充数据
【发布时间】:2015-01-03 13:06:46
【问题描述】:

在我的 WPF C# 项目中,使用来自 DB (Sqlite) 的数据填充 DataGrid 时遇到问题。

我已经在我的 XAML 文件中使用名称 (dataGrid_Students) 定义了 DataGrid:

<DataGrid Name="dataGrid_Students" Grid.Row="0" Grid.Column="0"
          RenderOptions.ClearTypeHint="Enabled"
          TextOptions.TextFormattingMode="Display"
          CanUserAddRows="False"
          CanUserDeleteRows="False"
          SelectionUnit="FullRow" 
          AutoGenerateColumns="false"  
          >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Index Number" />
        <DataGridTextColumn Header="First Name" /> 
        <DataGridTextColumn Header="Last Name" /> 
    </DataGrid.Columns>
</DataGrid>

在定义 DataGrid 组件的此窗口的构造函数中的 C# 文件中,我调用了方法 initDataGrid();

public StudentForm() {
    InitializeComponent();
    this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    initDataGrid();
}

其中包含此代码:

private void initDataGrid() { 
    //dataGrid_Student.DataContext = dbManager.Students; does not work
    dataGrid_Student.ItemsSource = dbManager.Students;
}

所以问题是我的 DataGrid 是空的。我检查了我的学生列表是否可能为空,但不是,它包含所有数据。那么为了用 Data 填充 DataGrid,我还缺少什么?

更新:

我的学生班级:

[Table("student")]
class Student {

    [Key] // this is a primary key in my table
    [Column(name:"brind")]
    public int Brind { get; set; }
    [Column(name:"first_name")]
    public string FirstName { get; set; }
    [Column(name:"last_name")]
    public string LastName { get; set; }
}

将我的属性从 Student 类绑定到此列:

<DataGrid Name="dataGrid_Student" Grid.Row="0" Grid.Column="0"
          RenderOptions.ClearTypeHint="Enabled"
          TextOptions.TextFormattingMode="Display"
          CanUserAddRows="False"
          CanUserDeleteRows="False"
          SelectionUnit="FullRow" 
          AutoGenerateColumns="false"  
          >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Index Number" Binding="{Binding Brind}" /> 
        <DataGridTextColumn Header="First Name"  Binding="{Binding FirstName}" />  
        <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" /> 
    </DataGrid.Columns>
</DataGrid>

DataGrid 仍然是空的。

我的 dbManager 实例是这个类:

class ManagerDBContext : DbContext {

    public ManagerDBContext() : base("Student_DbContext") {
        Database.SetInitializer<ManagerDBContext>(null);
    }
    public DbSet<User> Users { get; set; }
    public DbSet<Student> Students { get; set; }
}

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    您缺少列的绑定部分,因为您已将 AutoGenerateColumns 设置为 false,

    <DataGrid.Columns>
            <DataGridTextColumn Header="Index Number" Binding="{Binding IndexNumber}" />
            <DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"  /> 
            <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" /> 
    </DataGrid.Columns>
    

    绑定的时候还需要转成List

    private void initDataGrid() { 
        //dataGrid_Student.DataContext = dbManager.Students; does not work
        dataGrid_Student.ItemsSource = dbManager.Students.ToList();
    }
    

    【讨论】:

    • 列的绑定部分?我不熟悉绑定。你能给我举个例子吗?
    • @Kapparino 要绑定到 Student 属性的列必须在 xaml 中定义
    • 这个绑定变量,这个变量是在我的Student类中定义的属性吧?
    • 好的,让我试试看,等一下:)
    • @Kapparino 没错,试一试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多