0
votes

I need to export to XLS the selected row of my DevExpress GridView. When i try to export my grid, it creates an empty file. I think it is due to the callback that clear the data in my grid before exporting.

This is samples of my ASPx page.

The Load data button

 <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Styles/Images/load.png" onclick="ImageButton1_Click" />

The gridview

<dx:ASPxGridView ID="grid" ClientInstanceName="grid" runat="server" AutoGenerateColumns="False" KeyFieldName="car">
        <Columns>
            <dx:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0" Caption="">
            </dx:GridViewCommandColumn>
            <dx:GridViewDataTextColumn Caption="Category" VisibleIndex="1" FieldName="category"
                Name="category" GroupIndex="0" SortIndex="0" SortOrder="Ascending">
            </dx:GridViewDataTextColumn>
            <dx:GridViewDataTextColumn Caption="Car" VisibleIndex="2" FieldName="car" Name="car">
            </dx:GridViewDataTextColumn>
        </Columns>
        <SettingsPager PageSize="100">
        </SettingsPager>
        <Settings ShowFooter="True" />

        <GroupSummary>
            <dx:ASPxSummaryItem FieldName="car" SummaryType="Count" />
        </GroupSummary>

    </dx:ASPxGridView>

The GridExporter

    <dx:ASPxGridViewExporter ID="gridExport" runat="server" GridViewID="grid" ExportedRowType="Selected" />

The create file button

<dx:ASPxButton ID="createFile" runat="server" Text="Create File"      UseSubmitBehavior="False" OnClick="createFile_Click">
                    <Image Url="~/Styles/Images/save.png">
                    </Image>
    </dx:ASPxButton>

Now the code behind.

When I click the load button, i create fake datas for my tests.

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        CreateFakeData();
    }

I create my datas in a DataTable Object. And then i bind my dataTable with my grid.

grid.DataSource = fakes;
grid.DataBind();

Everything seem to works great but when i click the export button, nothing is exported.

    protected void createFile_Click(object sender, EventArgs e)
    {
        gridExport.WriteXlsToResponse();
    }

I followed the DevExpress tutoria to export selected rows in ASPl. But it seems that my page is refreshed so i loose the data binded with the grid before the export.

2

2 Answers

2
votes

I had a similar situation when I was binding data to an ASPXGridView in the code behind. I was able to solve it by rebinding in the button click, before the export line.

protected void createFile_Click(object sender, EventArgs e)
{
    grid.DataSource = fakes;        
    grid.DataBind();        
    gridExport.WriteXlsToResponse();
}
2
votes

The problem was with the postback, as I expected, I needed a way to keep my gridView Datas somewhere and reload it after the postback.

I added the OnDataBinding attribute to my gridView

 <dx:ASPxGridView ID="grid" ClientInstanceName="grid" runat="server"
AutoGenerateColumns="False" Width="100%" Caption="My car collection"
OnDataBinding="grid_DataBinding" KeyFieldName="car_model">

In the code behind, I coded the grid-DataBinding function so it restores the data using the data stocked in the session previously.

  protected void grid_DataBinding(object sender, EventArgs e)
        {
            if (Session["gridData"] != null)
            {
                // Assign the data source in grid_DataBinding
                grid.DataSource = Session["gridData"];
            }
        }

Don't forget to save the data in the session when desired. (I did it right after the search)

 protected void searchBtn_Click(object sender, ImageClickEventArgs e)
        {

            datas = FindCars(input.Text);

            grid.DataSource = datas;
            grid.DataBind();

            //Store data in the session
            Session["gridData"] = this.datas;

        }