【问题标题】:WPF - fill data on data gridWPF - 在数据网格上填充数据
【发布时间】:2013-12-19 11:52:06
【问题描述】:

我有以下代码,但我没有找到任何方法来添加来自用户的数据 到表。我想动态地做到这一点。

我创建了 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);

【问题讨论】:

    标签: c# wpf wpf-controls wpfdatagrid


    【解决方案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;
    }
    

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

    【讨论】:

    • 非常感谢您的回答!!我是Java的新人,你能详细说明我应该怎么做吗?我应该在哪里定义观察者搭配以及如何定义?我应该在哪里定义 InotifyPropChange 以及如何定义?再次感谢!!!
    【解决方案2】:

    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>
    

    背后的代码

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

    【讨论】:

      【解决方案3】:

      我总是使用这段代码在我的 Datagrid 中显示/填充数据:

          private void LoadGrid()
          {
              SqlCommand cmd = new SqlCommand("Select * From XXX;", con);
              DataTable dt = new DataTable();
              con.Open();
              SqlDataAdapter sdr = new SqlDataAdapter(cmd);
              sdr.Fill(dt);
              dataGrid.ItemsSource = dt.DefaultView;
              con.Close();
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-15
        • 1970-01-01
        • 2020-11-17
        • 2013-11-02
        • 1970-01-01
        • 2014-09-26
        • 2011-02-05
        相关资源
        最近更新 更多