RSS

Monthly Archives: November 2011

telerik radgridview export to excel


which is user control  AgentReportGrid.xaml

 

<telerik:RadGridView Name=”radGridView1″ MinWidth=”800″ CanUserFreezeColumns=”False” CanUserResizeColumns=”False” VerticalAlignment=”Top” ColumnWidth=”0″  Style=”{StaticResource DefaultGridStyle}” ScrollMode=”RealTime” Grid.Row=”0″ RowLoaded=”radGridView1_RowLoaded”   >
<!–<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding=”{Binding callerfirstname}” Header=”First Name” MinWidth=”100″ />
<telerik:GridViewDataColumn DataMemberBinding=”{Binding callermiddlename}” Header=”Middle Name” MinWidth=”100″ />
<telerik:GridViewDataColumn DataMemberBinding=”{Binding callerlastname}” Header=”Last Name” MinWidth=”100″ />
<telerik:GridViewDataColumn DataMemberBinding=”{Binding src}” Header=”Caller Phone No.” MinWidth=”150″ />
<telerik:GridViewDataColumn DataMemberBinding=”{Binding dst}” Header=”Number” MinWidth=”150″ />
<telerik:GridViewDataColumn DataMemberBinding=”{Binding duration}” Header=”Duration” MinWidth=”160″  />

</telerik:RadGridView.Columns>–>

<i:Interaction.Behaviors>
<local:EmptyDataTemplateBehavior>
<local:EmptyDataTemplateBehavior.EmptyDataTemplate>
<DataTemplate>
<TextBlock Text=”No results found!” Foreground=”red”  HorizontalAlignment=”Center”  VerticalAlignment=”Center” />
</DataTemplate>
</local:EmptyDataTemplateBehavior.EmptyDataTemplate>
</local:EmptyDataTemplateBehavior>
</i:Interaction.Behaviors>

</telerik:RadGridView>

<telerik:RadDataPager x:Name=”radDataPager1″
PageSize=”20″
AutoEllipsisMode=”None”
DisplayMode=”First, Previous, Next, Text” Grid.Row=”1″  Source=”{Binding Items, ElementName=radGridView1}” />

 

you can fill itself with datasource.

StudentReportFilter.xaml which is also user control left side of the window

<telerik:RadButton x:Name=”btnExport”  Style=”{StaticResource DefaultButtonStyle}” Grid.Row=”14″ Width=”75″ Content=”Export” Grid.RowSpan=”2″ Click=”btnExport_Click” HorizontalAlignment=”Left” VerticalAlignment=”top”></telerik:RadButton>

 

StudentReportFilter.cs

CS File write this code behind

 

private void btnExport_Click(object sender, RoutedEventArgs e)
{
// if ((((System.Windows.Controls.ScrollViewer)(((System.Windows.Controls.Grid)(this.Parent)).Parent)).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.cdr.Count > 0)

if ((((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radGridView1.Items.Count > 0)
{

string extension = “xls”;
SaveFileDialog dialog = new SaveFileDialog()
{
DefaultExt = extension,
Filter = String.Format(“{1} files (*.{0})|*.{0}|All files (*.*)|*.*”, extension, “Excel”),
FilterIndex = 1
};
if (dialog.ShowDialog() == true)
{

int originalPageSize =  (((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radDataPager1.PageSize;
int originalPageIndex = (((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radDataPager1.PageIndex;

using (System.IO.Stream stream = dialog.OpenFile())
{
(((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radDataPager1.PageSize = 0;

Telerik.Windows.Controls.GridViewExportOptions options = new Telerik.Windows.Controls.GridViewExportOptions();
Telerik.Windows.Controls.ExportFormat format = Telerik.Windows.Controls.ExportFormat.ExcelML;
options.Format = format;

options.ShowColumnHeaders = true;

options.Encoding = System.Text.Encoding.UTF8;

options.ShowColumnHeaders = true;
// options.Items = (IEnumerable)(((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radDataPager1.PagedSource;

// name of the grid which you want export

(((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radGridView1.Export(stream, options);

}
(((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radDataPager1.PageSize = originalPageSize;
(((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radDataPager1.PageIndex = originalPageIndex;

}
}
else
{
MessageBox.Show(“There are no records to export.”, “Export Warning!”, MessageBoxButton.OK);
}
}

SetMyLeads.Silverlight.UI (which application name)
AgentReportingPage (which is page)

Advertisements
 
Leave a comment

Posted by on November 13, 2011 in Silverlight