silverlight RadGridView总结三

在RadGridView中进行分组以及导出

分组

主要是在前台进行分组的定义:

前台代码:

View Code
 <telerik:RadGridView x:Name="RadGridView1" ItemsSource="{Binding Customers}" CanUserFreezeColumns="False" AutoExpandGroups="True" AutoGenerateColumns="False">
             <telerik:RadGridView.GroupDescriptors>
                 <telerik:GroupDescriptor Member="Country">
                     <telerik:GroupDescriptor.AggregateFunctions>
                         <telerik:CountFunction Caption="Total customers:" />
                     </telerik:GroupDescriptor.AggregateFunctions>
                 </telerik:GroupDescriptor>
             </telerik:RadGridView.GroupDescriptors>
             <telerik:RadGridView.Columns>
                 <telerik:GridViewDataColumn Header="CustomerID" DataMemberBinding="{Binding CustomerID}" />
                 <telerik:GridViewDataColumn Header="CompanyName" DataMemberBinding="{Binding CompanyName}" />
                 <telerik:GridViewDataColumn Header="ContactName" DataMemberBinding="{Binding ContactName}" />
                 <telerik:GridViewDataColumn Header="ContactTitle" DataMemberBinding="{Binding ContactTitle}" />
                 <telerik:GridViewDataColumn Header="Country" DataMemberBinding="{Binding Country}" />
                 <telerik:GridViewDataColumn Header="City" DataMemberBinding="{Binding City}" />
             </telerik:RadGridView.Columns>
         </telerik:RadGridView>

 

导出---RadGridView控件自带导出功能(以导出EXCEL为例)

需在前台定义事件---然后在后台定义导出功能。(需要注意的是,将控件设置为隐藏的话导出是不成功的

定义的事件ElementExporting="控件名称_ElementExporting"

后台代码:

View Code
  private void 空间名称_ElementExporting(object sender, GridViewElementExportingEventArgs e)
         {
             if (e.Element == ExportElement.HeaderRow || e.Element == ExportElement.HeaderCell)
             {
                 e.Background = Colors.LightGray;
                 e.FontSize = 20;
                 e.FontWeight = FontWeights.Bold;
                 e.Height = 50;
                 e.Width = 100;
             }
         }


当然,你的UI中肯定有一个导出的Button的控件来导出,其后台的事件如下:

View Code
 private void Button控件名称_Click(object sender, RoutedEventArgs e)
         {
             string extensioin = "xls";
             ExportFormat format = ExportFormat.Html;
             SaveFileDialog dialog = new SaveFileDialog();
             dialog.DefaultExt = extensioin;
             dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extensioin, "Excel");
 
             dialog.FilterIndex = 1;
 
             if (dialog.ShowDialog() == true)
             {
                 try
                 {
                     using (Stream str = dialog.OpenFile())
                     {
                         GridViewExportOptions exportextension = new GridViewExportOptions();
                         exportextension.Format = format;
                         exportextension.Encoding = System.Text.Encoding.GetEncoding("utf-8");//放置乱码的出现
                         exportextension.ShowColumnFooters = true;
                         exportextension.ShowColumnHeaders = true;
                         exportextension.ShowGroupFooters = true;
                         this.SCityGridView.SalesRadGridView.Export(str, exportextension);
                     }
                 }
                 catch (Exception)
                 {
 
                    
                 }
                
             }
         }

你可能感兴趣的:(silverlight)