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

Export selected rows

1 Answer 136 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joslyn
Top achievements
Rank 1
Joslyn asked on 28 Oct 2013, 11:57 AM
Hi,
I tried many codes that showed how to make only selected rows export, but im not able to get it to work.can someone guide me on this.I want to export to excel.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 28 Oct 2013, 12:06 PM
Hi Joslyn,

When exporting selected rows,make sure you have set IgnorePaging=false.Please try the sample code snippet.

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 Excel" 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.ExportSettings.IgnorePaging = false;
    RadGrid1.ExportSettings.FileName = "Document"
    RadGrid1.MasterTableView.ExportToExcel();
}

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