Grid Exporting to Excel

5 posts, 1 answers
  1. Hector Hernandez
    Hector Hernandez avatar
    45 posts
    Member since:
    Oct 2009

    Posted 20 Apr 2014 Link to this post

    Hello.
    i have this issue, i have a RadGrid i populate with then event NeedDataSource, then i change some value of cells with the event ItemDataBound, but when i want to export the grid only export the columns that i fill wih NeedDataSource event.
    is there a way to put also the other columns that i have modify with ItemDataBound


    thanks in advance.
    Hector
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 21 Apr 2014 in reply to Hector Hernandez Link to this post

    Hello,

    Please try with the below code snippet.

    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName.Equals(RadGrid.ExportToExcelCommandName))
        {
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
            {
                item["YourColumnUniqueName"].Text = "YourNewValue";
            }
        }
    }

    There is not such type of issue exists in RadGrid. But if you want to change the value before export then please use above code snippet.

    Let me know if any concern.

    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Hector Hernandez
    Hector Hernandez avatar
    45 posts
    Member since:
    Oct 2009

    Posted 22 Apr 2014 in reply to Jayesh Goyani Link to this post

    Thanks Jayesh
    the code that you post dont work for me,
    i call the export event from a asp.imagebutton
    here the code how i do:

    string alternateText = (sender as ImageButton).AlternateText;
                GrdRegion.ExportSettings.Excel.Format = (GridExcelExportFormat)Enum.Parse(typeof(GridExcelExportFormat), alternateText);
                GrdRegion.ExportSettings.ExportOnlyData = true;
                GrdRegion.MasterTableView.GetColumn("EmpRnf").Display = true;
                GrdRegion.ExportSettings.FileName = "dd";
                GrdRegion.ExportSettings.OpenInNewWindow = true;
                GrdRegion.MasterTableView.ExportToExcel();

    hope you can help me.
  5. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 23 Apr 2014 in reply to Hector Hernandez Link to this post

    Hi Hector Hernandez,

    It is hard to identify the issue with so less information, please provide your full code snippet. Meanwhile check the below sample code snippet and try to resolve the issue.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1" AllowPaging="true" OnItemDataBound="RadGrid1_ItemDataBound">
        <MasterTableView DataKeyNames="OrderID" CommandItemDisplay="Top">
            <CommandItemTemplate>
                <br />
                <asp:ImageButton ID="imgbExport" runat="server" AlternateText="Export" OnClick="imgbExport_Click" />
                <br />
            </CommandItemTemplate>
            <Columns>
                <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="EmployeeID" DataField="EmployeeID" HeaderText="EmployeeID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="OrderDate" HeaderText="OrderDate" UniqueName="OrderDate">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT  * FROM [Orders]"></asp:SqlDataSource>

    C#:
    protected void imgbExport_Click(object sender, ImageClickEventArgs e)
    {
        RadGrid1.ExportSettings.ExportOnlyData = true;      
        RadGrid1.ExportSettings.FileName = "FileName";
        RadGrid1.ExportSettings.OpenInNewWindow = true;
        RadGrid1.MasterTableView.ExportToExcel();
    }
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = (GridDataItem)e.Item;
            if (dataItem["EmployeeID"].Text=="3")
            {
                dataItem["ShipCity"].Text = "Null";
            }
        }
    }

    Thanks,
    Princy
  6. Hector Hernandez
    Hector Hernandez avatar
    45 posts
    Member since:
    Oct 2009

    Posted 25 Apr 2014 in reply to Princy Link to this post

    Thank Princy, it works!!!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017