Search code examples
c#wpfdatagrid

WPF binding: DataGrid showing empty rows instead of my data


I have created a simple WPF application where I want to import data from an excel sheet, perform some custom actions and export to an online calendar. To have grip on the number of items exported to the online agenda I want to create a datagrid where I can filter and select items which I want to export.

I have the wpf grid as follows:

<DataGrid Name="dataGridAgendaItems" AutoGenerateColumns="False" ItemsSource="{Binding}"  Margin="0,0,0,61" >
  <DataGrid.Columns>
    <DataGridTextColumn Header="X" Width="0.3*" Binding="{Binding Path=IsSelected}" />
    <DataGridTextColumn Header="Datum" Width="1*" Binding="{Binding Path=Date}" />
    <DataGridTextColumn Header="Tijd" Width="1*" Binding="{Binding Path=Time}" />
    <DataGridTextColumn Header="Locatie" Width="1*" Binding="{Binding Path=Location}" />
    <DataGridTextColumn Header="Title"  Width="2*" Binding="{Binding Path=Title}" />
    <DataGridTextColumn Header="Omschrijving" Width="3*" Binding="{Binding Path=Description}" />
  </DataGrid.Columns>
</DataGrid>

The code behind (stripped for reviewing and help purpose:

namespace ExcelToCalendar
{
    public partial class MainWindow : Window
    {
        public ICollection<Excel.AgendaRow> AgendaItems;
        ...
        private void BtnConfirmColumns_Click(object sender, RoutedEventArgs e)
        {
            ...

            AgendaItems = new List<Excel.AgendaRow>();
            foreach(var row in Rows)
            {
                ...
                Excel.AgendaRow tmpAgendaItem = new()
                {
                    Date = row.Columns[dateColumn].ToString(),
                    Title = row.Columns[titleColumn].ToString()
                };
                ...
                AgendaItems.Add(tmpAgendaItem);
            }

            dataGridAgendaItems.ItemsSource = AgendaItems;
        }
    }
}

and the excel class

 public class Excel
    {
        ...

        public class AgendaRow
        {
            public int Id;
            public required string Date;
            public string? Time;
            public string? Location;
            public string? Description;
            public required string Title;
            public bool IsSelected;
        }
    }

When I run the application the datagrid shows the correct number of Rows, but all columns are empty. What is going wrong? Tried a lot of stuf for hours now and not getting somewhere, please help.


Solution

  • It looks like the issue might be related to the way you've defined your AgendaRow class. In WPF, when you bind data to a DataGrid, it relies on properties, not fields. Here's how you can modify your AgendaRow class to use properties:

    public class Excel
    {
        // ...
    
        public class AgendaRow
        {
            public int Id { get; set; }
            public string Date { get; set; }
            public string Time { get; set; }
            public string Location { get; set; }
            public string Description { get; set; }
            public string Title { get; set; }
            public bool IsSelected { get; set; }
        }
    }