Search code examples
infragisticsxamdatagrid

Top n Record from infragistics xamdatagrid in wpf


I have a wpf application in which I am using xamdatagrid of infragistics 14.2 to show data. Sometimes grid may contains more than 1000 record.In same application I have given print option to print grid data.for printing I am using Report object of Infragistics.but before printing I am generating preview of print data using xamprintpreview control.

When grid contains large data (for 1000 record) it takes 35 seconds to generate preview for 1000 records.

I am binding Datatable to xamdatagrid(so you can say every record is a type of DataRow).

To speedup the process of preview I have an idea to show only top n record of grid in preview.but I am not able to fetch top n record of grid and store them in a datatable because if I take 10 record from datatable that I have binded to grid and user has applied some filter on grid so my preview mismatch with actual grid.

So please help me to fetch tp 10 record from grid that is currently displayed and store them in datatable.

Thanks in advance...


Solution

  • You have a complete working example here The keys are:

    xamDataGrid.RecordManager.GetFilteredInDataRecords() gives your the filtered records. If you have not any filter it gives you all records. Then Take(10) will take ten as much. And I implemented ToDataTable as an extension method to convert the records taken to a datatable.

    <Window x:Class="Stackoverflow1.MainWindow"
            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"
            xmlns:igDP="http://infragistics.com/DataPresenter"
            xmlns:local="clr-namespace:Stackoverflow1"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <igDP:XamDataGrid x:Name="xamDataGrid" Loaded="XamDataGrid_Loaded">
                <igDP:XamDataGrid.FieldLayoutSettings>
                    <igDP:FieldLayoutSettings FilterUIType="LabelIcons"/>
                </igDP:XamDataGrid.FieldLayoutSettings>
    
                <igDP:XamDataGrid.FieldSettings>
                    <igDP:FieldSettings AllowRecordFiltering="True" FilterLabelIconDropDownType="MultiSelectExcelStyle"/>
                </igDP:XamDataGrid.FieldSettings>
                <igDP:XamDataGrid.FieldLayouts>
                    <igDP:FieldLayout>
                        <igDP:Field Name="Date"/>
                        <igDP:Field Name="Order"/>
                    </igDP:FieldLayout>
                </igDP:XamDataGrid.FieldLayouts>
            </igDP:XamDataGrid>
            <Button x:Name="btnPrintPreview" Content="Print Preview" Height="25" Width="100" Click="btnPrintPreview_Click"/>
        </Grid>
    </Window>
    
    using Infragistics.Windows.DataPresenter;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Data;
    using System.Linq;
    using System.Windows;
    
    namespace Stackoverflow1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void XamDataGrid_Loaded(object sender, RoutedEventArgs e)
            {
                ObservableCollection<Item> list = new ObservableCollection<Item>();
    
                for (int i = 0; i < 15; ++i)
                {
                    list.Add(new Item { Date = DateTime.Now.AddDays(-i), Order = i});
                }
    
                xamDataGrid.DataSource = list;
            }
    
            private void btnPrintPreview_Click(object sender, RoutedEventArgs e)
            {
                IEnumerable<DataRecord> data = xamDataGrid.RecordManager.GetFilteredInDataRecords();
                DataTable dt = data.Take(10).ToDataTable();
                // DO YOUR PRINT PREVIEW
            }
        }
    
        class Item
        {
            public DateTime Date { get; set; }
            public int Order { get; set; }
    
        }
    
        static class Utils
        {
            public static DataTable ToDataTable(this IEnumerable<DataRecord> items)
            {
                if (items.Count() > 0)
                {
                    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Item));
                    DataTable table = new DataTable();
    
                    List<Item> list = new List<Item>();
    
                    foreach (var item in items)
                    {
                        list.Add((Item)item.DataItem);
                    }
    
                    foreach (PropertyDescriptor prop in properties)
                        table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
    
                    foreach (Item item in list)
                    {
                        DataRow row = table.NewRow();
                        foreach (PropertyDescriptor prop in properties)
                            row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                        table.Rows.Add(row);
                    }
    
                    return table;
                }
    
                return null;
            }
        }
    }