WPF DataGrid 未填充数据
WPF DataGrid Is Not Filled With Data
我在使用 WPF C# 项目中的 DB (Sqlite) 数据填充 DataGrid 时遇到问题。
我在 XAML 文件中定义了 DataGrid,名称为 (dataGrid_Students):
<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>
在我的 C# 文件中,此 Window 的构造函数中定义了 DataGrid 组件,我调用了方法 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 是空的。我检查了我的学生列表是否可能为空,但它不是,它包含所有数据。那么,为了用数据填充 DataGrid,我还缺少什么?
更新:
我的学生class:
[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 class 绑定到此列:
<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:
class ManagerDBContext : DbContext {
public ManagerDBContext() : base("Student_DbContext") {
Database.SetInitializer<ManagerDBContext>(null);
}
public DbSet<User> Users { get; set; }
public DbSet<Student> Students { get; set; }
}
您缺少列的绑定部分,因为您已将 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();
}
我在使用 WPF C# 项目中的 DB (Sqlite) 数据填充 DataGrid 时遇到问题。
我在 XAML 文件中定义了 DataGrid,名称为 (dataGrid_Students):
<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>
在我的 C# 文件中,此 Window 的构造函数中定义了 DataGrid 组件,我调用了方法 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 是空的。我检查了我的学生列表是否可能为空,但它不是,它包含所有数据。那么,为了用数据填充 DataGrid,我还缺少什么?
更新:
我的学生class:
[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 class 绑定到此列:
<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:
class ManagerDBContext : DbContext {
public ManagerDBContext() : base("Student_DbContext") {
Database.SetInitializer<ManagerDBContext>(null);
}
public DbSet<User> Users { get; set; }
public DbSet<Student> Students { get; set; }
}
您缺少列的绑定部分,因为您已将 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();
}