silverlight RadGridView总结三
在RadGridView中进行分组以及导出
分组
主要是在前台进行分组的定义:
前台代码:
View Code
1 <telerik:RadGridView x:Name="RadGridView1" ItemsSource="{Binding Customers}" CanUserFreezeColumns="False" AutoExpandGroups="True" AutoGenerateColumns="False"> 2 <telerik:RadGridView.GroupDescriptors> 3 <telerik:GroupDescriptor Member="Country"> 4 <telerik:GroupDescriptor.AggregateFunctions> 5 <telerik:CountFunction Caption="Total customers:" /> 6 </telerik:GroupDescriptor.AggregateFunctions> 7 </telerik:GroupDescriptor> 8 </telerik:RadGridView.GroupDescriptors> 9 <telerik:RadGridView.Columns> 10 <telerik:GridViewDataColumn Header="CustomerID" DataMemberBinding="{Binding CustomerID}" /> 11 <telerik:GridViewDataColumn Header="CompanyName" DataMemberBinding="{Binding CompanyName}" /> 12 <telerik:GridViewDataColumn Header="ContactName" DataMemberBinding="{Binding ContactName}" /> 13 <telerik:GridViewDataColumn Header="ContactTitle" DataMemberBinding="{Binding ContactTitle}" /> 14 <telerik:GridViewDataColumn Header="Country" DataMemberBinding="{Binding Country}" /> 15 <telerik:GridViewDataColumn Header="City" DataMemberBinding="{Binding City}" /> 16 </telerik:RadGridView.Columns> 17 </telerik:RadGridView>
导出---RadGridView控件自带导出功能(以导出EXCEL为例)
需在前台定义事件---然后在后台定义导出功能。(需要注意的是,将控件设置为隐藏的话导出是不成功的)
定义的事件ElementExporting="控件名称_ElementExporting"
后台代码:
View Code
1 private void 空间名称_ElementExporting(object sender, GridViewElementExportingEventArgs e) 2 { 3 if (e.Element == ExportElement.HeaderRow || e.Element == ExportElement.HeaderCell) 4 { 5 e.Background = Colors.LightGray; 6 e.FontSize = 20; 7 e.FontWeight = FontWeights.Bold; 8 e.Height = 50; 9 e.Width = 100; 10 } 11 }
当然,你的UI中肯定有一个导出的Button的控件来导出,其后台的事件如下:
View Code
1 private void Button控件名称_Click(object sender, RoutedEventArgs e) 2 { 3 string extensioin = "xls"; 4 ExportFormat format = ExportFormat.Html; 5 SaveFileDialog dialog = new SaveFileDialog(); 6 dialog.DefaultExt = extensioin; 7 dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extensioin, "Excel"); 8 9 dialog.FilterIndex = 1; 10 11 if (dialog.ShowDialog() == true) 12 { 13 try 14 { 15 using (Stream str = dialog.OpenFile()) 16 { 17 GridViewExportOptions exportextension = new GridViewExportOptions(); 18 exportextension.Format = format; 19 exportextension.Encoding = System.Text.Encoding.GetEncoding("utf-8");//放置乱码的出现 20 exportextension.ShowColumnFooters = true; 21 exportextension.ShowColumnHeaders = true; 22 exportextension.ShowGroupFooters = true; 23 this.SCityGridView.SalesRadGridView.Export(str, exportextension); 24 } 25 } 26 catch (Exception) 27 { 28 29 30 } 31 32 } 33 }