This is a migrated thread and some comments may be shown as answers.

[Solved] Export Custom Paging RadGrid

1 Answer 203 Views
Grid
This is a migrated thread and some comments may be shown as answers.
fahad
Top achievements
Rank 1
fahad asked on 23 Feb 2010, 10:48 AM
Hi,

I am using RadGrid binded to objectdatasource, I am trying to provide a facility to users to export the grid data to csv file.

the aspx file is as follow:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestRadGrid._Default" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"
            DataSourceID="ObjectDataSource1" GridLines="None" AllowCustomPaging="True"
            AllowPaging="True" AllowSorting="True"
            onitemcommand="RadGrid1_ItemCommand" >
        <MasterTableView CommandItemDisplay="Top" DataSourceID="ObjectDataSource1">
            <CommandItemSettings ShowExportToCsvButton="true" />
                 <Columns>
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <telerik:GridBoundColumn DataField="Age" HeaderText="Age" SortExpression="Age" />
                    <telerik:GridBoundColumn DataField="DOB" HeaderText="DOB" SortExpression="DOB" />
                 </Columns>
        </MasterTableView>
        </telerik:RadGrid>
    </div>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"  EnablePaging="True" SelectCountMethod = "getCount" SortParameterName = "sortExpression"
        SelectMethod="GetData" TypeName="TestRadGrid.DataClass">
        <SelectParameters>
            <asp:Parameter Name="criteria" Type="String" />
        </SelectParameters>
    </asp:ObjectDataSource>
    </form>
</body>
</html>

and code behind is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestRadGrid
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "ExportToCsv")
            {
                RadGrid1.VirtualItemCount = 12;
                RadGrid1.PageSize = 12;
                RadGrid1.ExportSettings.IgnorePaging = true;
                RadGrid1.ExportSettings.OpenInNewWindow = true;
                //RadGrid1.ExportSettings.ExportOnlyData = true;
                RadGrid1.ExportSettings.FileName = "Test";
                RadGrid1.MasterTableView.ExportToCSV();
            }
        }
    }
}


DataClass providing the methods to objectdatasource is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace TestRadGrid
{
    public class DataClass
    {
        public DataRow[] GetData(string criteria, int maximumRows, int startRowIndex, string sortExpression)
        {
            DataSet ds = new DataSet();
            
            ds.Tables.Add("Table");
            ds.Tables[0].Columns.Add("Id",System.Type.GetType("System.Int32"));
            ds.Tables[0].Columns.Add("Name");
            ds.Tables[0].Columns.Add("Age");
            ds.Tables[0].Columns.Add("DOB");
            DataRow nrow;
            for (int x = 0; x < 12; x++)
            {
                nrow=ds.Tables[0].NewRow();
                nrow["Id"] = x;
                nrow["Name"]="abc" + x.ToString();
                nrow["Age"] = x;
                nrow["DOB"] = DateTime.Now.AddDays(x);
                ds.Tables[0].Rows.Add(nrow);
            }

            int start=startRowIndex;
            int max=startRowIndex+maximumRows;
            DataRow[] rows = ds.Tables[0].Select("Id>=" + start.ToString() + " and Id<=" + max.ToString(), "");


            return rows;
        }

        public int getCount(string criteria)
        {
            return 12;
        }
        
    }
}

This code is not working, please check the above and advice what i am doing wrong here.

Regards,

Syed Fahad Anwar



1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 23 Feb 2010, 12:42 PM
Hi,

When you perform Custom paging you fetch only a fixed number of records based on the parameters such as startrow index and maximum rows. When you ignorePaging none of these parameters are passed. So you would need to add an extra checking in GetData method to return the entire number of records in such a case. Here is a sample from the demo link where I have added a checking to return all the rows when startRowIndex and maximumRows are 0.

C#:
 
public List<MyBusinessObject> Select(int startRowIndex, int maximumRows) 
    Random Random1 = new Random(); 
     
    List<MyBusinessObject> list = new List<MyBusinessObject>(); 
 
    int i = startRowIndex; 
    while (i < Math.Min(_maxItems, startRowIndex + maximumRows)) 
    { 
        MyBusinessObject Product1 = new MyBusinessObject(); 
        Product1.ID = i; 
        Product1.Name = names[Random1.Next(9)]; 
        Product1.UnitPrice = prizes[Random1.Next(9)]; 
        Product1.Date = dates[Random1.Next(9)]; 
        Product1.Discontinued = bools[Random1.Next(9)]; 
        list.Add(Product1); 
        i++; 
    } 
 
    if (startRowIndex == 0 && maximumRows == 0) 
    { 
 
        while (i < Math.Min(_maxItems, startRowIndex + 100)) 
        { 
            MyBusinessObject Product1 = new MyBusinessObject(); 
            Product1.ID = i; 
            Product1.Name = names[Random1.Next(9)]; 
            Product1.UnitPrice = prizes[Random1.Next(9)]; 
            Product1.Date = dates[Random1.Next(9)]; 
            Product1.Discontinued = bools[Random1.Next(9)]; 
            list.Add(Product1); 
            i++; 
        } 
    } 
    return list; 

Thanks,
Princy
Tags
Grid
Asked by
fahad
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or