0
votes

i am porting my application from windows forms to WPF. I have found that datagrid is available only through the WPF Toolkit. However i have some issues with performance. This could be because something that performed alright in windows forms does not in wpf since it has to be implemented differently. Anyhow, i am doing something like this:

// XAML
<toolkit:DataGrid x:Name="dataGrid" ItemsSource="{Binding Path=.}"/>


// C#
DataTable dataTable = new DataTable();
MyViewModel viewModel = new MyViewModel();

 this.dataGrid.AutoGenerateColumns = false;
 this.dataGrid.CanUserAddRows = false;
 this.dataGrid.CanUserDeleteRows = false;
 this.dataGrid.CanUserReorderColumns = true;
 this.dataGrid.CanUserResizeColumns = true;
 this.dataGrid.CanUserResizeRows = false;
 this.dataGrid.CanUserSortColumns = true;
 this.dataGrid.IsReadOnly = true;
 this.dataGrid.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
 this.dataGrid.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;

     this.dataGrid.ColumnHeaderHeight = 18;
     this.dataGrid.RowHeight = 18;
     this.dataGrid.DataContext = vm.dataGrid;

    List<string> s = new List<string>();

    for (int i = 0; i < 80; ++i)
    {
        s.Add("col" + i);
    }

    for (int i = 0; i < s.Count; ++i)
    {
        dataTable.Columns.Add(new DataColumn(s[i], typeof(string)));

        Binding items = new Binding();
        PropertyPath path = new PropertyPath(dataTable.Columns[i].ColumnName);
        items.Path = path;

        DataGridColumn dc = new DataGridTextColumn()
        {
            Header = dataTable.Columns[i].ColumnName,
            Width=70,
            Binding = items
        };

        this.dataGrid.Columns.Add(dc);
    }

  viewModel.dataGrid = dataTable;
  this.dataGrid.DataContext = viewModel.dataGrid;

  for (int i = 0; i < 1000; ++i)
  {
      var row = dataTable.NewRow();
      for (int j = 0; j < s.Count; ++j)
      {
          row[s[j]] = "text" + s[j] + j;
      }
      dataTable.Rows.Add(row);
  }

The above is a sample of simply generating same number of columns by binding each header. When this loads, i get all the elements displayed correctly, but scrolling is really slow. I know that someone might say that i am rendering 80 columns and 1000 elements, but i had more than 10000 displayed in my windows forms application and had no issues with it. The only difference with Windows Forms is that instead of using Binding i was setting the dataGridView's dataSource to the DataTable each time i was updating it. Any ideas? What am i doing wrong?

1

1 Answers

2
votes

Try setting EnableRowVirtualization=True