Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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:
C#
private void dgEmployee_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            try
            {
           if (DataSourceFordgEmployee.Count - 10 < e.Row.GetIndex())
            {
               

               List<Employee> Employees= GetEmployeeData();//GetEmployeeData is a simple method which returns list of 100 Employees

               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:

XML
<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>
                <!--1stcolumn as Sr.No-->
                <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>
        <!--<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="296,213,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />-->
    </Grid>
</UserControl>





MainPage.xaml.cs

C#
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;
            }

        }

        //LoadingRow="dgEmployee_LoadingRow" 
        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.
Posted
Updated 19-Apr-13 1:16am
v3
Comments
Idle_Force 19-Apr-13 22:37pm    
Why not use paging?

http://www.codeproject.com/Articles/83906/Silverlight-4-Datagrid-Sorting-Grouping-Filtering#
n.manish 23-Apr-13 6:44am    
This is a requirement

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900