1.默认数据显示(自动显示列)
后台绑定
//DataGrid 数据绑定
dataGridOne.ItemsSource = _Context.Info.ToList();
前台定义
<DataGrid Name="dataGridOne"
HorizontalAlignment="Left" Margin="10,92,0,0"
VerticalAlignment="Top" Height="219"
Width="642"/>
结果显示:
2.自定义显示列,DataGrid.AutoGenerateColumns 只是是否产生自动列
后台绑定同上
前台定义:
<DataGrid Grid.Row="1" AutoGenerateColumns="False"
x:Name="dataGridOne" Margin="20,20,20,20" >
<DataGrid.Columns>
<DataGridTextColumn Header="编号" Binding="{Binding Code}"/>
<DataGridTextColumn Header="姓名" Binding="{Binding Name}"/>
<DataGridTextColumn Header="性别" Binding="{Binding Sex}"/>
<DataGridTextColumn Header="民族" Binding="{Binding Nation}"/>
<DataGridTextColumn Header="生日" Binding="{Binding Birthday}"/>
</DataGrid.Columns>
</DataGrid>
结果显示:
DataGrid 简单数据绑定实例
时间: 2024-11-10 06:41:44