0
votes

I have an issue. I am trying to fill datagrid with data.

<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="54,74,0,0" VerticalAlignment="Top" Width="347" Height="206" />

I do load the data from database and add them to the grid like that?

 List<Employee> employees= Modules.OleDatabase.loadEmployeesInside();
            foreach (var empl in employees)
            {
                Debug.WriteLine(empl.Person_number);
                dataGrid.Items.Add(empl);
            }

But I just get this output in grid: enter image description here

Althougt all the data is definetly loaded right. Output in debug:

enter image description here

I did try using

ObservableCollection<Employee>

instead of list as well, but the same output. Can someone help me please?

My Employee class

public class Employee
{
    public int Person_number;
    public string Status, Name, Surname;
}

I did binding now

 <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="54,74,0,0" VerticalAlignment="Top" Width="347" Height="206">
        <DataGrid.Columns>
            <DataGridTextColumn Width="*" Header="Person_number" Binding="{Binding Person_number}" />
            <DataGridTextColumn Width="*" Header="Status" Binding="{Binding Status}" />
            <DataGridTextColumn Width="*" Header="Name" Binding="{Binding Name}" />
            <DataGridTextColumn Width="*" Header="Surname" Binding="{Binding Surname}" />
        </DataGrid.Columns>
    </DataGrid>

but the output is still like that.: enter image description here

2
Have you implemented IPropertyChanged? I don't seem to see it anywhere. - Sinnich
Thats new term for me, I will look into that - Tomáš Filip
@Sinnich well i it really necceseary? The grid should update itself on ItemSource change - Tomáš Filip
if Peregrine's answer isn't doing it for you, I would recommend looking into it. - Sinnich
Wpf is designed to work with MVVM pattern. Take a look upon www.learnmvvm.com, or use other web resources to get to know MVVM - Rekshino

2 Answers

0
votes

Just set the ItemsSource property of your DataGrid to the list of items.

The following works fine for me ...

DataItem.cs

public class DataItem
{
    public DataItem(int id, string description)
    {
        Id = id;
        Description = description;
    }

    public int Id { get; }
    public string Description { get; }
}

MainWindow.xaml

<DataGrid Margin="16"
              x:Name="TheDataGrid" />

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataItems = new ObservableCollection<DataItem>();
        TheDataGrid.ItemsSource = DataItems;
        LoadDataItems();
    }

    public ObservableCollection<DataItem> DataItems { get; }

    private void LoadDataItems()
    {
        DataItems.Add(new DataItem(1,"One"));
        DataItems.Add(new DataItem(2, "Two"));
        DataItems.Add(new DataItem(3, "Three"));
        DataItems.Add(new DataItem(4, "Four"));
        DataItems.Add(new DataItem(5, "Five"));
    }
}
0
votes

How does your Employee class look like? Seems like it doesn't have any Properties that the Grid can auto generate columns for.

Edit: You should be using Properties and not fields in the Employee class like this:

public class Employee
    {
        public int Person_number { get; set; }
        public string Status { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
    }