Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have defined a DataTemplate for one of my classes (MultiRunSimObj). This object also contains an ObservableCollection (SimObjects). I bind a DataGrid in my DataTemplate to this collection.

I want to be able to export this datagrid to excel file (preferable with all the formatting). I do not want to export the hidden columns though.

I can put a button for export on the DataTemplate, but don't know where to go after that. Since this is a DataTemplate, I do not have the luxury of code behind (anyways I want to avoid that for MVVM). Please guide.

Here is my DataTemplate (not showing the code for all the columns as there are > 100 in number).



XML
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:my="clr-namespace:Simulators;assembly=Simulators"
	xmlns:mv="clr-namespace:WPFResources;assembly=WPFResources"
					>
	<ResourceDictionary.MergedDictionaries>
		<ResourceDictionary Source="..\..\BaseStyles.xaml"/>
		<ResourceDictionary Source="..\..\Styles\Brushes.xaml"/>
		<ResourceDictionary Source="..\..\Styles\Buttons.xaml"/>
		
		<ResourceDictionary>
 
<DataTemplate DataType=  "{x:Type my:MultiRunSimObj}"  >
			  
				 
				<Grid IsEnabled="{Binding SimPending, Converter={StaticResource NotConvert}}">
 
					<DockPanel LastChildFill="True" >
 
						<Expander Header="Description" FontSize="12" DockPanel.Dock="Bottom" IsExpanded="false">
							<TextBox Name="txtDescription"  Height="50" VerticalScrollBarVisibility="Visible" AcceptsReturn="True" Foreground="blue"
						 Text="{Binding Path=Description}"/>
						</Expander>
						<StackPanel>
	
							<Grid>
 
								<DataGrid x:Name="GV_Run" ClipboardCopyMode="IncludeHeader" FrozenColumnCount="1"
								 ItemsSource="{Binding Path=SimObjects}" AutoGenerateColumns="False" RowDetailsVisibilityMode="Collapsed"
								 CanUserReorderColumns="False" CanUserSortColumns="False"
					  GridLinesVisibility="All" VerticalGridLinesBrush="#FF89C696" HorizontalGridLinesBrush="#FF89C696"
					  FontSize="12" FontStyle="Normal" FontFamily="Courier New" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" 
								 >
 
									<DataGrid.Resources>
										<mv:BindingProxy x:Key="proxy" Data="{Binding SimObj}" />
 
<Style TargetType="{x:Type DataGridRowHeader}">
											<Setter Property="Template">
												<Setter.Value>
													<ControlTemplate TargetType="DataGridRowHeader">
														<Border Padding="2">
															<StackPanel Orientation="Horizontal">
 
																<Button Name="btnLaunchObj"  ToolTip="Settings" Height="16" Width="16"
												Style="{StaticResource SettingsButtonStyle}"
														Command="{Binding LaunchControlModule}" Margin="0,1,2,1">
 
																</Button>
																<Popup x:Name="popupCurrentObj" 
													StaysOpen="False"     
													PlacementTarget="{Binding ElementName=UCMain}"
													Placement="Center"
													PopupAnimation="Fade"
													IsOpen="{Binding ShowControlModule}"   
													>
 
																	<Border BorderBrush="Maroon" BorderThickness="2" removed="#FFEA9D" Padding="5">
																		<ContentControl Content="{Binding}"/>
																	</Border>
 
																</Popup>
															</StackPanel>
														</Border>
 
													</ControlTemplate>
 
												</Setter.Value>
											</Setter>
										</Style>
 
										<Style TargetType="DataGridRow">
											<Style.Triggers>
												<DataTrigger Binding="{Binding strRunState}" Value="0">
													<Setter Property="Background" Value="White" />
												</DataTrigger>
												<DataTrigger Binding="{Binding strRunState}" Value="1">
													<Setter Property="Background" Value="LightGreen" />
												</DataTrigger>
												<DataTrigger Binding="{Binding strRunState}" Value="2">
													<Setter Property="Background" Value="Red" />
												</DataTrigger>
										   
											</Style.Triggers>
										</Style>
 
									</DataGrid.Resources>
 
									<DataGrid.RowDetailsTemplate>
										<DataTemplate>
											<ContentControl Content="{Binding}"/>
										</DataTemplate>
									</DataGrid.RowDetailsTemplate>
 
									<DataGrid.Columns>
										
 
										<!--Inputs-->
 
										<!--Run ID-->
										<DataGridTextColumn Header="Run ID" Binding="{Binding Path=RunID, Mode=TwoWay}"/>
                                   
                                        
                        <!--INDOOR UNIT INPUTS-->
 
										<!--DBi-->
                                        <DataGridTextColumn  Visibility="{Binding  Data.ControlModule.BoundaryConditions.IU_BCs.selDBi,
						Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}"  Header="DBi" Binding="{Binding Path=ControlModule.BoundaryConditions.IU_BCs.DBi, Mode=TwoWay}"/>
 
										<!--WBi-->
                                        <DataGridTextColumn Header="{Binding Data.ControlModule.BoundaryConditions.IU_BCs.GetMoistAirPropDisplay, Source={StaticResource proxy}}" Visibility="{Binding  Data.ControlModule.BoundaryConditions.IU_BCs.selAMoistPropValue,
						Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Binding="{Binding Path=ControlModule.BoundaryConditions.IU_BCs.AMoistPropValue, Mode=TwoWay}"/>
 
										<!--IU Airflow-->
										<DataGridTextColumn Header="{Binding Data.ControlModule.BoundaryConditions.IU_BCs.GetAirflowDisplay,  Source={StaticResource proxy}}" Visibility="{Binding  Data.ControlModule.BoundaryConditions.IU_BCs.selAirflow,
						Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Binding="{Binding Path=ControlModule.BoundaryConditions.IU_BCs.AirflowValue, Mode=TwoWay}"/>
 
                                        <!--Patm_Alt-->
                                        <DataGridTextColumn Header="{Binding Data.ControlModule.BoundaryConditions.IU_BCs.GetPatm_AltDisplay,  Source={StaticResource proxy}}" Visibility="{Binding  Data.ControlModule.BoundaryConditions.IU_BCs.selPatm_AltValue,
						Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Binding="{Binding Path=ControlModule.BoundaryConditions.IU_BCs.Patm_AltValue, Mode=TwoWay}"/>
                                        
                                        <!-- ESP-->
                                        <DataGridTextColumn Visibility="{Binding  Data.ControlModule.BoundaryConditions.IU_BCs.selESP,
						Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}"  Header="ESP" Binding="{Binding Path=ControlModule.BoundaryConditions.IU_BCs.ESP, Mode=TwoWay}"/>
 
 
                                        
                        <!--Coil1 Air Coil IU INPUTS-->
 
                                        <!--Coil1 DBi-->
                                        <DataGridTextColumn  Visibility="{Binding  Data.ControlModule.AirCoil1BC.selDBi,
						Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}"  Header="Coil1 DBi" Binding="{Binding Path=ControlModule.AirCoil1BC.DBi, Mode=TwoWay}"/>
 
                                        <!--Coil1 WBi-->
                                        <DataGridTextColumn Header="{Binding Data.ControlModule.AirCoil1BC.GetMoistAirPropDisplay, Converter={StaticResource DisplayConverter}, ConverterParameter=Coil1 ;, Source={StaticResource proxy}}" Visibility="{Binding  Data.ControlModule.AirCoil1BC.selAMoistPropValue,
						Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Binding="{Binding Path=ControlModule.AirCoil1BC.AMoistPropValue, Mode=TwoWay}"/>
 
                                        <!--Coil1 IU Airflow-->
                                        <DataGridTextColumn Header="{Binding Data.ControlModule.AirCoil1BC.GetAirflowDisplay,  Source={StaticResource proxy}}" Visibility="{Binding  Data.ControlModule.AirCoil1BC.selAirflow,
						Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Binding="{Binding Path=ControlModule.AirCoil1BC.AirflowValue, Mode=TwoWay}"/>
 
                                        <!--Coil1 Patm_Alt-->
                                        <DataGridTextColumn Header="{Binding Data.ControlModule.AirCoil1BC.GetPatm_AltDisplay,  Source={StaticResource proxy}}" Visibility="{Binding  Data.ControlModule.AirCoil1BC.selPatm_AltValue,
						Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Binding="{Binding Path=ControlModule.AirCoil1BC.Patm_AltValue, Mode=TwoWay}"/>
 
                                        <!--Coil1 ESP-->
                                        <DataGridTextColumn Visibility="{Binding  Data.ControlModule.AirCoil1BC.selESP,
						Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}"  Header="Coil1 ESP" Binding="{Binding Path=ControlModule.AirCoil1BC.ESP, Mode=TwoWay}"/>
 


                                 </DataGrid.Columns>
									
								</DataGrid>
 
 
 
								
							</Grid>
					
 
						</StackPanel>
 
 
 
 
					</DockPanel>
				</Grid>
 
			</DataTemplate>
 
			
		</ResourceDictionary>
		
	</ResourceDictionary.MergedDictionaries>
	  
 
 
 
</ResourceDictionary>
Posted
Updated 31-Jan-14 7:46am
v4

Use the VisualTreeHelper and get all the data into an array.
msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper(v=vs.110).aspx
 
Share this answer
 
Comments
jainga10213 31-Jan-14 13:49pm    
Hi Kenneth,
Thanks for your reply. I am new to VisualTreeHelper. Is there a good resource where I can find some working examples for the same?
Will this approach also export the entire datagrid or just the portion of it that is visible (some of it may not be visible due to horizontal scrolling)?
How do you decide what is hidden?
Use the ObservableCollection itself, but do not consider exporting columns that are visible.
 
Share this answer
 
Comments
jainga10213 31-Jan-14 13:52pm    
Hi Abinav,
Each of my property within the class has a corresponding boolean property that decides whether it is visible or not. I will try to explore this approach further. One question,is there a way to export the formatting also through this approach?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900