Export exception

3 posts, 0 answers
  1. Jerry
    Jerry avatar
    70 posts
    Member since:
    Jan 2011

    Posted 28 Sep 2011 Link to this post

    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. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 29 Sep 2011 Link to this post

    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 >>

  3. Jerry
    Jerry avatar
    70 posts
    Member since:
    Jan 2011

    Posted 18 Oct 2011 Link to this post

    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
Back to Top