WPF-在数据网格上填充数据 [英] WPF - fill data on data grid

查看:105
本文介绍了WPF-在数据网格上填充数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,但没有找到如何将用户
中的数据添加到表中的任何方法。我想动态地进行操作。

I have the following code and I didnt find any way how to add the data from users to table .I want to do that dynamically .

我已经创建了WPF应用程序,并添加了数据网格和按钮,当我单击按钮上的
看到数据网格上的数据时,我应该如何继续?

I have create WPF application and add Data Grid and button and I that when I click on the button see the data on the data grid ,how should I proceed?

private void Button_Click(object sender, RoutedEventArgs e)
{
    //get user data...
    DataTable dt = new DataTable("Users Info");
    DataGrid.ItemsSource = dt.DefaultView;

    foreach (var user in users)
    {
        string firstName = user.Firstname;
        string lastName = user.Lastname;
    }

 

Xaml是:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPFTest01" x:Class="WPFTest01.MainWindow"
        Title="WPF_DEMO_Main" Height="350" Width="525">

    <Grid x:Name="Grid" Background="#FF1BA1E2">
        <DataGrid x:Name="DataGrid" HorizontalAlignment="Left" Margin="65,60,0,0" VerticalAlignment="Top" Height="185" Width="385" SelectionChanged="DataGrid_SelectionChanged_1"/>
        <Button Content="Button" HorizontalAlignment="Left" Margin="390,280,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>

    </Grid>
</Window>

我尝试了其中一些代码,但未成功:(

I have tried with some of this code without success :(

//dt.Columns.Add("First Name", typeof(string));
//dt.Columns.Add("Last Name", typeof(string));

//DataSet ds = new DataSet();

//ds.Tables.Add(dt);


推荐答案

用户(集合)分配为DataGrid的ItemsSource在datagrid上显示数据。

Assign users (collection) as a ItemsSource of DataGrid to display the data on datagrid.

XAML

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="500">
    <StackPanel>
        <Grid x:Name="Grid" Background="#FF1BA1E2">
            <DataGrid x:Name="DataGrid" HorizontalAlignment="Left" Margin="65,10,0,0" VerticalAlignment="Top" Height="180" Width="385" SelectionChanged="DataGrid_SelectionChanged_1"/>
            <Button Content="Button" HorizontalAlignment="Left" Margin="95,235,0,0" VerticalAlignment="Top" Width="75" Height="30" Click="Button_Click_1"/>
        </Grid>
    </StackPanel>
</Window>

后面的代码

//Fill data here
private void Button_Click_1(object sender, RoutedEventArgs e)
{

            ObservableCollection<User> users = new ObservableCollection<User>();
            users.Add(new User{FirstName = "firstname-1",LastName = "lastname-1"});
            users.Add(new User{FirstName = "firstname-2",LastName = "lastname-2"});
            users.Add(new User{FirstName = "firstname-3",LastName = "lastname-3"});
            users.Add(new User{FirstName = "firstname-4",LastName = "lastname-4"});
            DataGrid.ItemsSource = users;
}

就是这样。

或者,您可以定义集合属性并将其绑定到DataGrid,而不是从代码隐藏中设置datagrid ItemsSource属性。

Alternatively you can define a collection property and bind it to DataGrid, instead of setting datagrid ItemsSource property from codebehind.

这篇关于WPF-在数据网格上填充数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆