How to use the DataGrid?

fangeles
Posts: 52
Joined: Wed Jan 16, 2019 12:48 am

How to use the DataGrid?

Postby fangeles » Thu Mar 07, 2019 4:47 pm

Hello,

I am using the DataGrid control and having a slowdowns when setting the ItemSource with many records.

Please see my sample code below.

Code: Select all

<DataGrid x:Name="DataGrid1" AutoGenerateColumns="False">
   <DataGrid.Columns>
      <DataGridTextColumn Header="Name" Binding="{Binding PersonName}"/>
      <DataGridTextColumn Header="Age" Binding="{Binding PersonAge}"/>
      <DataGridTextColumn Header="Address" Binding="{Binding PersonAddress}"/>
      <DataGridTextColumn Header="Contact Number" Binding="{Binding PersonContactNumber}"/>
   </DataGrid.Columns>
</DataGrid>



Then my C# code.

Code: Select all

public class Person
{
   public string PersonName { get; set; }
   public int PersonAge{ get; set; }
   public string PersonAddress{ get; set; }
   public string PersonContactNumber{ get; set; }
}


private void FillDataGrid()
{
   List<Person> Listing = new List<Person>();
   
   //This loop adds let say 1000 items to the list
   for(int index = 0; index < 1000; index++)
   {
      Person current = new Person();
      current = GenerateDummyRecord();
      Listing.Items.Add(current);
   }
   
   //Sets the Listing as the item source of DataGrid
   DataGrid1.ItemsSource = Listing;
}



But as what I observed, the rendering of the DataGrid (loading, going to previous or next page of the DataGrid) is slow and will hang the UI.

Am I using the correct method?
Will wait for your feedback.

Thanks.

Fernan

Return to “General Discussion and Other”

Who is online

Users browsing this forum: No registered users and 1 guest