Click here to Skip to main content
15,886,258 members

Silverlight DataGrid progressively data loading

n.manish asked:

Open original thread
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.
Tags: Silverlight4, Silverlight (Silverlight4)

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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