How to access row details while exporting?

2 posts, 0 answers
  1. uvuvuv
    uvuvuv avatar
    3 posts
    Member since:
    Dec 2019

    Posted 17 Feb Link to this post

    Dear Developers,

    Here's a code which exports a RadGridView:

    ```
    private void grid1_ElementExported(object sender, GridViewElementExportedEventArgs e)
    {
        if (e.Element == ExportElement.Row)
        {
            GetData_Result obj = e.Context as GetData_Result;
            if (obj != null)
            {
                e.Writer.Write(String.Format(@"<tr><td colspan=""{0}"">", grid1.Columns.Count));
                // Here's row details for row being exported
                e.Writer.Write("</td></tr>");
            }
        }
    }

     

    private void bExport_Click(object sender, RoutedEventArgs e)
    {
        SaveFileDialog dialog = new SaveFileDialog()
        {
            DefaultExt = extension,
            Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", "xls", "Excel"),
            FilterIndex = 1
        };
        if (dialog.ShowDialog() == true)
        {
            using (Stream stream = dialog.OpenFile())
            {
                grid1.Export(stream,
                 new GridViewExportOptions()
                 {
                     Format = ExportFormat.Html, // Is it possible to use another format in this situation?
                     ShowColumnHeaders = true,
                     ShowColumnFooters = true,
                     ShowGroupFooters = false
                 });
            }

            Process.Start(new ProcessStartInfo(dialog.FileName));
        }
    }
    ```

    RowDetails is a DataTemplate which is filled by a method below when "+" button is clicked.

    ```

    private void grid1_RowDetailsVisibilityChanged(object sender, Telerik.Windows.Controls.GridView.GridViewRowDetailsEventArgs e)
    {
        if (e.Visibility != Visibility.Visible)
            return;

        GetDataD_Result data = e.Row.Item as GetDataD_Result;

        dvm = new DetailsViewModel(data.Id);
        e.DetailsElement.DataContext = dvm;
    }

    ```

     

    So, is possible to get an access to this DataTemplate of RwoDetails while exporting for each row?

    Thank you!

     

    Best regards

  2. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    1139 posts

    Posted 20 Feb Link to this post

    Hello,

    Thank you for the provided code snippets.

    One idea that comes to mind is to place the row details DataTemplate in the Resources collection of the RadGridView control and access it from there.

                <telerik:RadGridView.Resources>
                    <DataTemplate x:Key="DetailsTemplate">
                        <!-- ... -->
                    </DataTemplate>
                </telerik:RadGridView.Resources>
    You can also obtain the details element as you do in the RowDetailsVisibilityChanged handler:
            private void grid1_ElementExported(object sender, GridViewElementExportedEventArgs e)
            {
                if (e.Element == ExportElement.Row)
                {
                    GetData_Result obj = e.Context as GetData_Result;
                    if (obj != null)
                    {
                        e.Writer.Write(String.Format(@"<tr><td colspan=""{0}"">", grid1.Columns.Count));
                        // Here's row details for row being exported
                        var detailsTemplate = grid1.Resources["DetailsTemplate"] as DataTemplate;
                        var detailsElement = = new DetailsViewModel(obj.Id);
                        e.Writer.Write("</td></tr>");
                    }
                }
            }
    Please let me know whether such an approach would work for you. If that is not the case, please provide more details on your exact requirement so that I can better assist you. If possible, please send over a small sample project which demonstrates your current setup so that I can get a fuller understanding of your scenario.

    Regards,
    Dilyan Traykov
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top