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

Access RadGridView inside DataTemplate inside ResourceDictionary

1 Answer 146 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sagar
Top achievements
Rank 1
Sagar asked on 10 Aug 2015, 07:30 PM

My application is based on MVVM pattern. I want to access ​a RadGridView control/object, within Code-Behind of ResourceDictionary xaml to be able to use its Export method to export contents to an Excel file. 

There's a UserControl XAML file which merges its resources with few individual ResourceDictionary xaml files. One of them is "ProdUnitResource.xaml" which has a DataTemplate control (x:key="ProdUnitVM"). This DataTemplate contains RadGridView control (Name="ProdUnitGrid". Cannot use x:key attribute since the parent control complains).

So, basically this is the structure -->

<UserControl>    ProdUnit.xaml

     <ResourceDictionary>    ProdUnitResource.xaml  (want to access "ProdUnitGrid" in code behind file ProdUnitResource.xaml.cs)

            <DataTemplate x:key="ProdUnitVM">

                  <RadGridView Name="ProdUnitGrid">

              

 I want to be export contents of this grid to an Excel file on click of an Hyperlink called "Export". So, I created a code-behind file for the ResourceDictionary xaml called "ProdUnitResource.xaml.cs" and was able to add following click event handler method to it. 

private void Export_Click(object sender, System.Windows.RoutedEventArgs e)
{       ExportFormat format = ExportFormat.ExcelML;
 
        var dialog = new System.Windows.Forms.SaveFileDialog();
        dialog.DefaultExt = "xls";
        dialog.Filter = String.Format("Excel files (*.{0})|*.{0}|All files (*.*)|*.*", extension);
        dialog.FilterIndex = 1;
 
        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            using (Stream stream = dialog.OpenFile())
            {
                GridViewExportOptions exportOptions = new GridViewExportOptions();
                exportOptions.Format = format;
                exportOptions.ShowColumnHeaders = true;
                ProdUnitGrid.Export(stream, exportOptions);
            }
        }​
}

Click Event handling works fine but I am unable to directly use name of the RadGridView control "ProdUnitGrid" within code behind. It is not recognized. I want to access this RadGridView object so that I can call its Export method. Please share if you know how I can access it. 

 

1 Answer, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 13 Aug 2015, 03:37 PM
Hi,

Actually controls that are contained in DataTemplates does not actually exist before the DataTemplate has its content loaded. DataTemplates are not actual visual trees, but they are the blueprints for creating them. Thus you can reuse one and the same DataTemplate multiple times and you will have one separate GridVIew instance for each use.
Taking this into account you have to access the RadGridView instance that you want to export, as soon as the DataTemplate gets loaded. Does the button that you mentioned are in the same view with the same Windows/UseControl. If so you can traverse the visual tree to find the GridView that you need just before executing the export logic:
private void Export_Click(object sender, System.Windows.RoutedEventArgs e)
{     
        . . .
        if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            using (Stream stream = dialog.OpenFile())
            {
                . . .
                var grid = this.ChildrenOfType<RadGridView>().FirstOrDefault(. . .);
                ProdUnitGrid.Export(stream, exportOptions);
            }
        }​
}


Regards,
Ivan Ivanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Sagar
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Share this question
or