首页 文章

WPF - 填充数据网格上的数据

提问于
浏览
2

我有以下代码,我没有找到任何方式如何将数据从用户添加到表 . 我想动态地这样做 .

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

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>

我尝试了一些这些代码没有成功:(

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

//DataSet ds = new DataSet();

//ds.Tables.Add(dt);

2 回答

  • 3

    users (集合)指定为DataGrid的ItemsSource以在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>
    

    Code behind

    //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属性 .

  • 1

    首先定义一个集合属性:

    // Implement INotifyPropertyChanged interface correctly in this class
    public ObservableCollection<User> Users { get; set; }
    

    获取您的数据并填写此集合以响应 Button.Click

    foreach (User user in users)
    {
        Users.Add(user);
    }
    

    现在 Bind 它到XAML:

    <DataGrid ItemsSource="{Binding Users}" />
    

    而已!使用WPF时,忘记动态地执行它......这就是WinForms风格 .


    更新>>>

    初学者最简单(但不是最佳)的方法是定义属性并在 MainWindow.xaml 文件后面的代码中实现INotifyPropertyChanged interface . 接下来,将其添加到构造函数中:

    public MainWindow()
    {
        DataContext = this;
    }
    

    好的,这次真的是这样! :)

相关问题