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
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