0

My question is somewhat related to:

Wpf datagrid scrollbar freezes

After adding a data table to a data grid:

<DataGrid Name="appliedJobs" Background="#50000000" Grid.Row="1" Grid.Column="1" Margin="10,10,10,10" ItemsSource="{Binding dataTable.DefaultView}" AutoGenerateColumns="True" />

private void GenerateTableButton_Click(object sender, RoutedEventArgs e) 
{
     string table = readStuffFromFile();
     // Parse stuff read from file into a table 
     DataTable dataTable = InitializeTable(table);
     // Set binding
     appliedJobs.ItemsSource = dataTable.AsDataView();
}

Scrolling sideways is fine, but whenever I get to the bottom of the table the app freezes (100% CPU use (per thread)). Why?

The table structure is not fixed. It can be anything:

id     |   date
---------------------
1      |   13.05.2020
2      |   10.05.2020
3      |   18.05.2020

or

id     |   position   | requirements 
------------------------------------
1      |   dev        | 5 years exp
2      |   tech lead  | 8 years exp
3      |   dev        | 4 years exp

The DataTable should thus be dynamically created. Most samples found on SO use a fixed class for headers and values.

Sebi
  • 4,262
  • 13
  • 60
  • 116
  • Try changing that to appliedJobs.ItemsSource = dataTable.DefaultView; – Andy Oct 01 '20 at 13:05
  • Tried it. Same (UI hangs when scroll hits bottom. – Sebi Oct 01 '20 at 13:07
  • How many rows are in this datatable? – Andy Oct 01 '20 at 13:11
  • I cannot know in advance. Control flow: click button --> pick latex file --> add parsing and table creation in OnClick handler --> bind table to UI. – Sebi Oct 01 '20 at 13:14
  • Similar to https://stackoverflow.com/questions/16759603/datatable-to-observable-collection but I cannot use a specific class but need to work directly with a DataTable instance. – Sebi Oct 01 '20 at 13:15
  • How many rows do you get with this test data? Also. Use binding rather than setting itemssource directly. You need to set datacontext and you need a public property to bind to. Set that property to your datatable's defaultview. There are some circumstances will stop a datagrid from virtualising. I would always bind so i can't recall if setting itemssource would have that effect. – Andy Oct 01 '20 at 13:27
  • The number of rows/cols is determined at runtime after parsing a file. I already have a DataTable that is not bound to anything observable inside the DataGrid. – Sebi Oct 01 '20 at 13:50
  • 2
    @Sebi: You still haven't answered the question...how many rows are there when the `DataGrid` freezes? – mm8 Oct 01 '20 at 14:13
  • 11 consistent with DataTable number of rows but they are all blank. The datacontext is the current data context (this). – Sebi Oct 01 '20 at 20:45

0 Answers0