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

How to access row details while exporting?

1 Answer 61 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
uvuvuv
Top achievements
Rank 1
uvuvuv asked on 17 Feb 2020, 03:21 PM

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

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 20 Feb 2020, 12:40 PM

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.
Tags
General Discussions
Asked by
uvuvuv
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Share this question
or