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

[Solved] Export to excel not exporting items that once were editable

1 Answer 143 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Hans
Top achievements
Rank 1
Hans asked on 14 May 2013, 06:49 PM
Hi,

In the PreRender event of our Radgrid we have a loop to make some rows, one column, editable:
protected void RadGridFailureReportSearch_PreRender(object sender, EventArgs e)
 {
     string strPreviousFailureReportNbr = string.Empty;
 
     foreach (GridItem item in RadGridFailureReportSearch.MasterTableView.Items)
     {
         GridEditableItem editedItem1 = (GridEditableItem)item;
         if (editedItem1.GetDataKeyValue("ID").ToString() != strPreviousFailureReportNbr)
         {
             strPreviousFailureReportNbr = editedItem1.GetDataKeyValue("ID").ToString();
             if (item is GridEditableItem)
             {
                 GridEditableItem editableItem = item as GridDataItem;
                 editableItem.Edit = true;
             }
         }
         else
         {
             if (item is GridEditableItem)
             {
                 GridEditableItem editableItem = item as GridDataItem;
                 editableItem.Edit = false;
             }
         }
     }
     RadGridFailureReportSearch.Rebind();
 }

Having added an Export to Excel button to the grid I notice that the items that were set to Edit = true does not get exported.

I tried the following 
protected void RadGridFailureReportSearch_ItemCommand(object sender, GridCommandEventArgs e)
 {
     if (e.CommandName == RadGrid.ExportToExcelCommandName)
     {
         RadGridFailureReportSearch.ExportSettings.IgnorePaging = true;
         foreach (GridItem item in RadGridFailureReportSearch.MasterTableView.Items)
         {
             if (item is GridEditableItem)
             {
                 GridEditableItem editableItem = item as GridDataItem;
                 editableItem.Edit = false;
             }
         }
// Tried with and without below line
         RadGridFailureReportSearch.Rebind();
     }
 }

But that did not help. It seems that once an item have been editable it can not be exported using the built in export functionality.

Is this really so or is it just ignorance on my part? Any ideas?

Regards,
Hans

1 Answer, 1 is accepted

Sort by
0
Hans
Top achievements
Rank 1
answered on 15 May 2013, 05:03 AM
Found a solution to this problem. Tought I'd share it.

The reason that it did nt help to set the items to readonly in the ItemCommand handler was that the grid's PreRender function was called when pushing the export button.

Hence the fields were once again rendered as before.

The solution:

I created a boolean flag that I set to true like this:
protected void RadGridFailureReportSearch_ItemCommand(object sender, GridCommandEventArgs e)
{
            if (e.CommandName == RadGrid.ExportToExcelCommandName)
            {
                exportToExcelFlag = true;
                RadGridFailureReportSearch.ExportSettings.IgnorePaging = true;
            }
}

Then in the PreRender event handler I check this flag and set all items to readonly if the flag is set to true.

The flag is initially set to false in the PageLoad event handler (when not postback) and at some point the page is reloaded so that the flag is again set to false later in the export process. 

The PreRender event handler was actually called 4 or 5 times after having pushed the export button.

Here's the new code for my PreRender event handler:
protected void RadGridFailureReportSearch_PreRender(object sender, EventArgs e)
{
    string strPreviousFailureReportNbr = string.Empty;
 
    foreach (GridItem item in RadGridFailureReportSearch.MasterTableView.Items)
    {
        if (exportToExcelFlag)
        {
            if (item is GridEditableItem)
            {
                GridEditableItem editableItem = item as GridDataItem;
                editableItem.Edit = false;
            }
        }
        else
        {
            GridEditableItem editedItem1 = (GridEditableItem)item;
            if (editedItem1.GetDataKeyValue("ID").ToString() == strPreviousFailureReportNbr)
            {
                if (item is GridEditableItem)
                {
                    GridEditableItem editableItem = item as GridDataItem;
                    editableItem.Edit = false;
                }
            }
            else
            {
                strPreviousFailureReportNbr = editedItem1.GetDataKeyValue("ID").ToString();
                if (item is GridEditableItem)
                {
                    GridEditableItem editableItem = item as GridDataItem;
                    editableItem.Edit = true;
                }
            }
        }
    }
    RadGridFailureReportSearch.Rebind();
}


Regards,
Hans
Tags
Grid
Asked by
Hans
Top achievements
Rank 1
Answers by
Hans
Top achievements
Rank 1
Share this question
or