Hello All,
I am trying to export selected values from my grid to a CSV. All I can manage is to export the entire grid values.
These two links show how to do it.
http://www.telerik.com/community/forums/aspnet-ajax/grid/exporting-only-selected-rows.aspx
http://www.telerik.com/community/forums/aspnet-ajax/grid/exporting-selected-radgrid-rows-to-excel-csv.aspx
But, unfortunately I am not able to achieve that. I want to export only the rows selected and if none of the rows are selected, I want to export the entire grid. Here is my code
Any help is really appreciated.
I am trying to export selected values from my grid to a CSV. All I can manage is to export the entire grid values.
These two links show how to do it.
http://www.telerik.com/community/forums/aspnet-ajax/grid/exporting-only-selected-rows.aspx
http://www.telerik.com/community/forums/aspnet-ajax/grid/exporting-selected-radgrid-rows-to-excel-csv.aspx
But, unfortunately I am not able to achieve that. I want to export only the rows selected and if none of the rows are selected, I want to export the entire grid. Here is my code
protected
void
ButtonGo_Click(
object
sender, EventArgs e)
{
if
(DropDownList2.SelectedValue ==
"ExportToCSV"
)
{
foreach
(GridDataItem item
in
RadGrid1.MasterTableView.Items)
{
if
(!item.Selected)
{
item.Visible =
false
;
}
}
RadGrid1.ExportSettings.IgnorePaging =
true
;
RadGrid1.ExportSettings.FileName =
"CanvasData"
;
RadGrid1.MasterTableView.ExportToCSV();
}
}
Any help is really appreciated.
15 Answers, 1 is accepted
0

Shinu
Top achievements
Rank 2
answered on 28 Oct 2013, 04:55 AM
Hi Satish,
Please try setting IgnorePaging=false.
C#:
Thanks,
Shinu
Please try setting IgnorePaging=false.
C#:
RadGrid1.ExportSettings.IgnorePaging =
false
;
Thanks,
Shinu
0

Smart
Top achievements
Rank 1
answered on 28 Oct 2013, 03:25 PM
Hi Shinu,
Thank you for the reply, but, the suggestion of setting IgnorePaging=false. returns only the header texts and no data at all. What is wrong here?
Thank you,
Satish
Thank you for the reply, but, the suggestion of setting IgnorePaging=false. returns only the header texts and no data at all. What is wrong here?
Thank you,
Satish
0

Shinu
Top achievements
Rank 2
answered on 29 Oct 2013, 03:16 AM
Hi Satish,
Below is a sample code snippet that i tried which works as expected,can you give it a try and let me know.
ASPX:
C#:
Thanks,
Shinu
Below is a sample code snippet that i tried which works as expected,can you give it a try and let me know.
ASPX:
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
AutoGenerateColumns
=
"false"
AllowMultiRowSelection
=
"true"
>
<
MasterTableView
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"Column1"
HeaderText
=
"Column1"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"Column2"
HeaderText
=
"Column2"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"Column3"
HeaderText
=
"Column3"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"Column4"
HeaderText
=
"Column4"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"Column5"
HeaderText
=
"Column5"
>
</
telerik:GridBoundColumn
>
</
Columns
>
</
MasterTableView
>
<
ClientSettings
>
<
Selecting
AllowRowSelect
=
"true"
/>
</
ClientSettings
>
</
telerik:RadGrid
>
<
asp:Button
Text
=
"Export to CSV"
ID
=
"Button1"
OnClick
=
"Button1_Click"
runat
=
"server"
/>
C#:
protected
void
RadGrid1_NeedDataSource(
object
sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
DataTable table =
new
DataTable();
table.Columns.Add(
"Column1"
);
table.Columns.Add(
"Column2"
);
table.Columns.Add(
"Column3"
);
table.Columns.Add(
"Column4"
);
table.Columns.Add(
"Column5"
);
for
(
int
i = 0; i < 20; i++)
{
table.Rows.Add(
"Col1Row"
+ i,
"Col2Row"
+ i,
"Col3Row"
+ i,
"Col4Row"
+ i,
"Col5Row"
+ i);
}
RadGrid1.DataSource = table;
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
foreach
(GridDataItem item
in
RadGrid1.MasterTableView.Items)
{
if
(!item.Selected)
item.Visible =
false
;
}
RadGrid1.MasterTableView.Columns[0].Visible =
false
;
RadGrid1.MasterTableView.ShowHeadersWhenNoRecords =
true
;
RadGrid1.ExportSettings.ExportOnlyData =
true
;
RadGrid1.ExportSettings.IgnorePaging =
false
;
RadGrid1.ExportSettings.FileName =
"Details"
;
RadGrid1.MasterTableView.ExportToCSV();
}
Thanks,
Shinu
0

Smart
Top achievements
Rank 1
answered on 30 Oct 2013, 05:54 PM
Hello Shinu,
The requirement changed just a little bit i.e. created RadGrid dynamically from code behind, previously it was declarative, but the issue of not being able to export remains. Please see the code below which incorporated the requirement change.
C#
But, I see that the RadGrid1.MasterTableView.Items says the enumeration is empty. I even tried RadGrid1.SelectedItems but I realize that my grid doesn't even recognize the check box checked. Any suggestions?
Thank you in advance
The requirement changed just a little bit i.e. created RadGrid dynamically from code behind, previously it was declarative, but the issue of not being able to export remains. Please see the code below which incorporated the requirement change.
C#
protected
void
ButtonGo_Click(
object
sender, EventArgs e)
{
// RadGrid RadGrid1;
RadGrid RadGrid1= RadGridFormat(); // RadGrid is created dynamically here, with the client setting
etc enabled;
if
(DropDownList2.SelectedValue ==
"ExportToCSV"
)
{
foreach
(GridDataItem item
in
RadGrid1.MasterTableView.Items)
{
if
(!item.Selected)
{
item.Visible =
false
;
}
}
RadGrid1.MasterTableView.Columns[0].Visible =
false
;
RadGrid1.MasterTableView.ShowHeadersWhenNoRecords =
true
;
RadGrid1.ExportSettings.ExportOnlyData =
true
;
RadGrid1.ExportSettings.IgnorePaging =
false
;
RadGrid1.ExportSettings.FileName =
"Details"
;
RadGrid1.Rebind();
PlaceHolder1.Controls.Add(RadGrid1);
RadGrid1.MasterTableView.ExportToCSV();
}
}
Thank you in advance
0

Shinu
Top achievements
Rank 2
answered on 31 Oct 2013, 05:30 AM
Hi Satish,
I have noticed that you have created the RadGrid on a button click, please create the RadGrid in the Page_Init event and also remove the
RadGrid1.Rebind() from the button click. Below is a sample code snippet, please try and let me know if any concern.
ASPX:
C#:
Thanks,
Shinu
I have noticed that you have created the RadGrid on a button click, please create the RadGrid in the Page_Init event and also remove the
RadGrid1.Rebind() from the button click. Below is a sample code snippet, please try and let me know if any concern.
ASPX:
<
asp:PlaceHolder
ID
=
"PlaceHolder1"
runat
=
"server"
></
asp:PlaceHolder
>
<
asp:Button
ID
=
"Button1"
runat
=
"server"
Text
=
"Export To CSV"
OnClick
=
"Button1_Click1"
/>
C#:
RadGrid RadGrid1;
protected
void
Page_Init(
object
source, System.EventArgs e)
{
Loadprogramaticgrid();
RadGrid1.NeedDataSource +=
new
GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
}
private
void
Loadprogramaticgrid()
{
RadGrid1 =
new
RadGrid();
RadGrid1.ID =
"RadGrid1"
;
RadGrid1.MasterTableView.DataKeyNames =
new
string
[] {
"OrderID"
};
RadGrid1.Skin =
"Default"
;
RadGrid1.Width = Unit.Percentage(100);
RadGrid1.AutoGenerateColumns =
false
;
RadGrid1.AllowPaging =
true
;
RadGrid1.AllowSorting =
true
;
RadGrid1.AllowMultiRowSelection =
true
;
RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
RadGrid1.ClientSettings.Selecting.AllowRowSelect =
true
;
//Add columns
GridBoundColumn boundColumn;
boundColumn =
new
GridBoundColumn();
RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField =
"CustomerID"
;
boundColumn.HeaderText =
"CustomerID"
;
boundColumn =
new
GridBoundColumn();
RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField =
"ShipCity"
;
boundColumn.HeaderText =
"ShipCity"
;
boundColumn =
new
GridBoundColumn();
RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField =
"ShipVia"
;
boundColumn.HeaderText =
"ShipVia"
;
this
.PlaceHolder1.Controls.Add(RadGrid1);
}
protected
void
RadGrid1_NeedDataSource(
object
sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = loaddata(
"SELECT OrderID,CustomerID,ShipCity,ShipVia FROM Orders"
);
}
protected
void
Button1_Click1(
object
sender, EventArgs e)
{
foreach
(GridDataItem item
in
RadGrid1.MasterTableView.Items)
{
if
(!item.Selected)
{
item.Visible =
false
;
}
}
RadGrid1.MasterTableView.Columns[0].Visible =
false
;
RadGrid1.MasterTableView.ShowHeadersWhenNoRecords =
true
;
RadGrid1.ExportSettings.ExportOnlyData =
true
;
RadGrid1.ExportSettings.IgnorePaging =
false
;
RadGrid1.ExportSettings.FileName =
"Details"
;
PlaceHolder1.Controls.Add(RadGrid1);
RadGrid1.MasterTableView.ExportToCSV();
}
public
DataTable loaddata(
string
query)
{
String ConnString = ConfigurationManager.ConnectionStrings[
"Northwind_newConnectionString3"
].ConnectionString;
SqlConnection conn =
new
SqlConnection(ConnString);
SqlDataAdapter adapter =
new
SqlDataAdapter();
adapter.SelectCommand =
new
SqlCommand(query, conn);
DataTable myDataTable =
new
DataTable();
conn.Open();
try
{
adapter.Fill(myDataTable);
}
finally
{
conn.Close();
}
return
myDataTable;
}
Thanks,
Shinu
0

Smart
Top achievements
Rank 1
answered on 31 Oct 2013, 01:48 PM
Hello Shinu,
Thank you so much for the reply. But I get Null object reference at the line.
before calling the Loadprogramaticgrid(, I get no error but then no data is displayed.
This thing is supposed to be easy, but why am I struggling so much.
Please help me mate!
Thank you
Thank you so much for the reply. But I get Null object reference at the line.
RadGrid1.NeedDataSource +=
new
GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
And I can see why I get that error, but if I do something like RadGrid1=
new
RadGrid();
This thing is supposed to be easy, but why am I struggling so much.
Please help me mate!
Thank you
0

Shinu
Top achievements
Rank 2
answered on 01 Nov 2013, 03:32 AM
Hi Satish,
Can you move the code from Loadprogramaticgrid() to Page_Init() event and check if the issue exist. If this doesn't help, please provide your full code along with the steps to reproduce your issue.
C#:
Thanks,
Shinu
Can you move the code from Loadprogramaticgrid() to Page_Init() event and check if the issue exist. If this doesn't help, please provide your full code along with the steps to reproduce your issue.
C#:
protected
void
Page_Init(
object
source, System.EventArgs e)
{
RadGrid1 =
new
RadGrid();
RadGrid1.ID =
"RadGrid1"
;
. . . .
RadGrid1.NeedDataSource +=
new
GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
//Add columns
. . . .
this
.PlaceHolder1.Controls.Add(RadGrid1);
}
Thanks,
Shinu
0

Smart
Top achievements
Rank 1
answered on 08 Nov 2013, 06:28 PM
Hi Shinu,
Sorry for the late reply. On debugging I realized that, item.selected is always false. I am using the following code to create check boxes in the grid. So, even though the check box is checked and the export shows the value as true the item.selected is false.
So the main problem the check box selection is not the same as selecting the Row. Is there a work around?
Thank you
Sorry for the late reply. On debugging I realized that, item.selected is always false. I am using the following code to create check boxes in the grid. So, even though the check box is checked and the export shows the value as true the item.selected is false.
GridClientSelectColumn SelectColumn =
new
GridClientSelectColumn();
SelectColumn.UniqueName =
"ClientSelectColumn"
;
RadGrid1.MasterTableView.Columns.Add(SelectColumn);
RadGrid1.ClientSettings.Selecting.AllowRowSelect =
true
;
RadGrid1.ClientSettings.Selecting.EnableDragToSelectRows =
true
;
So the main problem the check box selection is not the same as selecting the Row. Is there a work around?
Thank you
0
Accepted

Shinu
Top achievements
Rank 2
answered on 11 Nov 2013, 06:15 AM
Hi ,
I'm not sure what is causing the issue, the code works fine at my end. I have added the GridClientSelectColumn to the Grid. Please try the following code snippet. If this doesn't help, please provide your full code snippet.
C#:
Thanks,
Shinu
I'm not sure what is causing the issue, the code works fine at my end. I have added the GridClientSelectColumn to the Grid. Please try the following code snippet. If this doesn't help, please provide your full code snippet.
C#:
RadGrid RadGrid1;
protected
void
Page_Init(
object
source, System.EventArgs e)
{
Loadprogramaticgrid();
RadGrid1.NeedDataSource +=
new
GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
}
private
void
Loadprogramaticgrid()
{
RadGrid1 =
new
RadGrid();
RadGrid1.ID =
"RadGrid1"
;
RadGrid1.MasterTableView.DataKeyNames =
new
string
[] {
"OrderID"
};
RadGrid1.AllowMultiRowSelection =
true
;
RadGrid1.ClientSettings.Selecting.AllowRowSelect =
true
;
RadGrid1.ClientSettings.Selecting.EnableDragToSelectRows =
true
;
//Add columns
GridClientSelectColumn SelectColumn =
new
GridClientSelectColumn();
SelectColumn.UniqueName =
"ClientSelectColumn"
;
RadGrid1.MasterTableView.Columns.Add(SelectColumn);
this
.PlaceHolder1.Controls.Add(RadGrid1);
}
protected
void
RadGrid1_NeedDataSource(
object
sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = loaddata(
"SELECT OrderID,CustomerID,ShipCity,ShipVia FROM Orders"
);
}
protected
void
Button1_Click1(
object
sender, EventArgs e)
{
foreach
(GridDataItem item
in
RadGrid1.MasterTableView.Items)
{
// If you want to export only the Checked Rows
CheckBox check = (CheckBox)item[
"ClientSelectColumn"
].Controls[0];
if
(check.Checked==
false
)
{
item.Visible =
false
;
}
//To Export Selected rows Uncomment the following code
/*if (!item.Selected)
{
item.Visible = false;
}*/
}
RadGrid1.ExportSettings.ExportOnlyData =
true
;
RadGrid1.ExportSettings.IgnorePaging =
false
;
PlaceHolder1.Controls.Add(RadGrid1);
RadGrid1.MasterTableView.ExportToCSV();
}
Thanks,
Shinu
0

Smart
Top achievements
Rank 1
answered on 11 Nov 2013, 05:19 PM
Thank you Shinu, exporting check box checked works, but the selection does not work. I am good with check box working.
0

Shinu
Top achievements
Rank 2
answered on 12 Nov 2013, 04:43 AM
Hi,
I couldn't replicate the issue at my end. This is not an expected behavior, can you paste your full code snippet so as to identify the issue.
Thanks,
Shinu
I couldn't replicate the issue at my end. This is not an expected behavior, can you paste your full code snippet so as to identify the issue.
Thanks,
Shinu
0

Smart
Top achievements
Rank 1
answered on 12 Nov 2013, 01:30 PM
Hi Shinu,
Sure will upload, please give me sometime before I do that
thank you
Sure will upload, please give me sometime before I do that
thank you
0
Hi,
I prepared a small runnable web site based on the Shinu suggestion and it is working properly on my side. Could you please give it a try and let me know how it differs from your real setup.
Regards,
Kostadin
Telerik
I prepared a small runnable web site based on the Shinu suggestion and it is working properly on my side. Could you please give it a try and let me know how it differs from your real setup.
Regards,
Kostadin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0

Vaibhav
Top achievements
Rank 1
answered on 08 Sep 2014, 12:16 PM
Hi.
I need to export only selected rows to excel.
I am able to hide columns before exporting.
I have searched a lot regarding hiding unselected rows/Exporting only selected rows in telerik threads.
In all my search results I found the below line of code.
​ if (Grid.SelectedItems.Count != 0)
{
foreach (GridDataItem item in Grid.MasterTableView.Items)
{
if (!item.Selected)
item.Visible = false;
}
} My excel format is"BIFF".
Its very urgent.
Please help
I need to export only selected rows to excel.
I am able to hide columns before exporting.
I have searched a lot regarding hiding unselected rows/Exporting only selected rows in telerik threads.
In all my search results I found the below line of code.
​ if (Grid.SelectedItems.Count != 0)
{
foreach (GridDataItem item in Grid.MasterTableView.Items)
{
if (!item.Selected)
item.Visible = false;
}
} My excel format is"BIFF".
Its very urgent.
Please help
0
Hi Vaibhav,
I prepared a small sample and attached it to this thread. Please give it a try and let me know about the result.
Regards,
Kostadin
Telerik
I prepared a small sample and attached it to this thread. Please give it a try and let me know about the result.
Regards,
Kostadin
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.