Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - fill data on data grid

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 .

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;
    }

 

The Xaml is:

<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);
like image 248
07_05_GuyT Avatar asked Feb 15 '23 12:02

07_05_GuyT


2 Answers

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>

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;
}

That's it.

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

like image 71
Ramashankar Avatar answered Feb 19 '23 20:02

Ramashankar


First define a collection property:

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

Get your data and fill this collection in response to the Button.Click:

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

Now Bind it to the XAML:

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

That's it! When working with WPF, just forget about doing it dynamically... that's WinForms style.


UPDATE >>>

The simplest (but not optimal) way for a beginner to get this working is to define your properties and implement the INotifyPropertyChanged interface in the code behind of the MainWindow.xaml file. Next, add this into the constructor:

public MainWindow()
{
    DataContext = this;
}

Ok, that's really it this time! :)

like image 28
Sheridan Avatar answered Feb 19 '23 21:02

Sheridan