i m creating a project which has a DataGrid which will have ten thousands of record,i was thinking of loading them progressively,i.e Initially only 100 rows will be fetched ,when the user scrolls down and reached around 90th the next 100 rows will be loaded.
For this i m using the loading row event of DataGrid the loading row method looks like this:
private void dgEmployee_LoadingRow(object sender, DataGridRowEventArgs e)
{
try
{
if (DataSourceFordgEmployee.Count - 10 < e.Row.GetIndex())
{
List<Employee> Employees= GetEmployeeData();
DataSourceFordgEmployee.AddRange(Employees);
}
}
catch (Exception)
{
throw;
}
}
This code works but there is a problem.
If the user scrolls down in grid using arrow keys everything is fine but id the scroll bar is used JIT Debugger exception comes up stating some out of range exception.
I am pasting the test project code
MainPage.Xaml:
<UserControl x:Class="StealthPagingSLApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" removed="White">
<sdk:DataGrid Height="112" HorizontalAlignment="Left" Margin="20,71,0,0" Name="dgEmployee" VerticalAlignment="Top" Width="353" AutoGenerateColumns="False" LoadingRow="dgEmployee_LoadingRow">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" x:Name="EmployeeId"
Header="Id" Width="*" Binding="{Binding EmployeeId,Mode=TwoWay}" IsReadOnly="True" />
<sdk:DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" x:Name="EmployeeName"
Header="Name" Width="*" Binding="{Binding Name,Mode=TwoWay}" IsReadOnly="True" />
<sdk:DataGridTextColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" x:Name="Contact"
Header="Contact" Width="*" Binding="{Binding ContactNumber,Mode=TwoWay}" IsReadOnly="True" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>
</UserControl>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace StealthPagingSLApp
{
public partial class MainPage : UserControl
{
List<Employee> DataSourceFordgEmployee = new List<Employee>();
bool _loading = false;
int PageSize = 10;
public MainPage()
{
try
{
InitializeComponent();
dgEmployee.ItemsSource = DataSourceFordgEmployee;
List<Employee> Employees = GetEmployeeData();
DataSourceFordgEmployee.AddRange(Employees);
}
catch (Exception)
{
throw;
}
}
private void dgEmployee_LoadingRow(object sender, DataGridRowEventArgs e)
{
try
{
if (DataSourceFordgEmployee.Count - 5 < e.Row.GetIndex())
{
List<Employee> Employees= GetEmployeeData();
DataSourceFordgEmployee.AddRange(Employees);
}
}
catch (Exception)
{
throw;
}
}
List<Employee> GetEmployeeData()
{
try
{
List<Employee> lReturnList = new List<Employee>();
int lCount = DataSourceFordgEmployee.Count;
for (int i = 0; i < 30; i++)
{
Employee lObj = new Employee();
lObj.EmployeeId = lCount;
lObj.Name = "FirstName";
lObj.ContactNumber = "1234" + lCount.ToString();
lReturnList.Add(lObj);
lCount++;
}
return lReturnList;
}
catch (Exception)
{
throw;
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
List<Employee> lEmployee = GetEmployeeData();
DataSourceFordgEmployee.AddRange(lEmployee);
dgEmployee.SelectedItem = DataSourceFordgEmployee[0];
}
catch (Exception)
{
throw;
}
}
}
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string ContactNumber { get; set; }
}
}
To simulate the problem do the following steps
1)When the MainPage is Loaded select the first row in the grid then go down the grid using down arrow key.You will see that the grid data keeps on increasing and everything is fine
2)Try using the Mouse Scroll or Vertical scroll bar of the grid and you will get a JIT Debugger exception.
Any idea why this happens
Thanks in advance.