Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF DataGrid and multithreading
    text
    copied!<p>I have a DataGrid that I fill with a DataView (using DataContext). I have tried to do that in a separate thread but the UI still freezes. I want to prevent the UI from freezing when populating the DataGrid.</p> <p>Here is the code I made so far:</p> <pre><code>private void btnOK_Click(object sender, RoutedEventArgs e) { GetFieldsBLL getFieldsBLL = new GetFieldsBLL(); DataView dv = getFieldsBLL.GetWholeView(ViewName); Task task = new Task(() =&gt; ucDataExtracViewControl.PopulateGrid(dv)); task.Start(); } public void PopulateGrid(DataView dv) { dgView.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate{ dgView.Columns.Clear(); dgView.AutoGenerateColumns = false; foreach (DataColumn column in dv.Table.Columns) { var gridColumn = new DataGridTextColumn() { Header = column.ColumnName, Binding = new Binding("[" + column.ColumnName + "]") }; dgView.Columns.Add(gridColumn); } dgView.DataContext = dv; DataView = dv; })); } </code></pre> <p>Thanks in advance!</p> <p>Edit: The reason why I recreate the columns is because some of the column names have a dot in their name. For instance “Job No.” doesn’t yield any data when using binding. Read more here: <a href="https://stackoverflow.com/questions/2940618/what-is-it-about-datatable-column-names-with-dots-that-makes-them-unsuitable-for">What is it about DataTable Column Names with dots that makes them unsuitable for WPF&#39;s DataGrid control?</a> It is not an option to make changes to the database. –</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload