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

Export exception

2 Answers 72 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jerry
Top achievements
Rank 1
Jerry asked on 29 Sep 2011, 03:19 AM
Hi there,


I have implemented the export function for the grid. Well, sometime it works fine, but sometime when I open page, and first time click the Export, it gives an exception, but the second time click works fine. see attached picture.

Could you explain what's the cause for this problem and how do I prevent this to happen?



regards
Jerry

2 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 29 Sep 2011, 06:55 AM
Hello,

 You can check this thread for more info:
http://stackoverflow.com/questions/1355078/dialogs-must-be-user-initiated-with-savefiledialog-in-silverlight-3

Greetings,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Jerry
Top achievements
Rank 1
answered on 19 Oct 2011, 04:39 AM
Hi Vlad,


Sorry about late reply. I don't think the link that you provided actually solve my problem.

Upon button click, I immediately show the popup which exactly as link described.

here is my button click event
private void SurveyCommandPanel_ExportClick(object sender, RoutedEventArgs e)
{
    CommonHelper.Export(SurveyAnswerGridView, ExportFormat.ExcelML);
}


Then following is the actualy Export method

public static void Export(GridViewDataControl grid, ExportFormat format)
{
    if (grid == null)
    {
        return;
    }
 
    if (format == ExportFormat.ExcelML || format == ExportFormat.Html)
    {
        grid.ElementExporting += (sender, e) =>
        {
            if (e.Format == ExportFormat.ExcelML)
            {
                if (e.Value != null && e.Value.GetType() == typeof(string))
                {
                    //e.ShouldEncodeValue = true;
 
                    bool shouldEncode = true;
 
                    foreach (var item in _specialChar)
                    {
                        if (e.Value.ToString().Contains(item))
                        {
                            shouldEncode = false;
                            break;
                        }
                    }
 
                    if (shouldEncode)
                    {
                        e.Value = HttpUtility.HtmlEncode((e.Value as string));
                    }
                }
            }
        };
    }
 
    string extension = string.Empty;
 
    switch (format)
    {
        case ExportFormat.ExcelML:
            extension = "xml";
            break;
        case ExportFormat.Html:
            extension = "doc";
            break;
        case ExportFormat.Csv:
            extension = "csv";
            break;
    }
 
    if (extension.IsNullOrEmpty())
    {
        throw new Exception("Unsuported ExportFormat, the extension can not be resolved.");
    }
 
    var dialog = new SaveFileDialog()
    {
        DefaultExt = extension,
        Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, format),
        FilterIndex = 1
    };
 
    if (dialog.ShowDialog() == true)
    {
        using (Stream stream = dialog.OpenFile())
        {
            grid.Export(stream,
             new GridViewExportOptions()
             {
                 Format = format,
                 ShowColumnHeaders = true,
                 ShowColumnFooters = true,
                 ShowGroupFooters = false,
             });
        }
    }
}


Well, could you tell me whats the problem could be?


regards
Jerry Ren
Tags
GridView
Asked by
Jerry
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Jerry
Top achievements
Rank 1
Share this question
or