WPF中经常需要将数据库离线数据集DataSet数据绑定到DataGrid中,前台代码:
<DataGrid Name="userGrid" HorizontalAlignment="Left" Margin="10,10,0,0" AutoGenerateColumns="False" VerticalAlignment="Top" Height="113" Width="477">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding userID}"/>
<DataGridTextColumn Header="姓名" Binding="{Binding userName}"/>
<DataGridTextColumn Header="密码" Binding="{Binding userPass}"/>
<DataGridTextColumn Header="角色" Binding="{Binding userRole}"/>
</DataGrid.Columns>
</DataGrid>
后台绑定语句:
DataSet ds = users.GetAllList(); //ds就是离线数据集数据集
userGrid.ItemsSource = ds.Tables[0].DefaultView;//userGrid是Datagrid
调试效果图如下: