接着上一篇WPF之 DataGrid数据绑定,继续讲述WPF中DataGrid分页。
由于分页经常用到,就做了一个自定义控件,由于当时的局限性,只支持DataTable数据源,不过木关系,网上很多其他数据类型转换成DataTable的方法,下面我提供一种List转换成DataTable的方法:
/// <summary> /// 将List转换成DataTable /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> public static DataTable ToDataTable<T>(this IList<T> data) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); DataTable dt = new DataTable(); for (int i = 0; i < properties.Count; i++) { PropertyDescriptor property = properties[i]; dt.Columns.Add(property.Name, property.PropertyType); } object[] values = new object[properties.Count]; foreach (T item in data) { for (int i = 0; i < values.Length; i++) { values[i] = properties[i].GetValue(item); } dt.Rows.Add(values); } return dt; }
废话不多说,先上一个分页的效果图:
分页布局代码如下:
<Grid> <Border CornerRadius="3" Background="Transparent" BorderBrush="#01544A" BorderThickness="1"> <Grid HorizontalAlignment="Stretch" Margin="5 0 1 0" VerticalAlignment="Top" Width="Auto" Height="25"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"/> <ColumnDefinition Width="300*" MinWidth="300"/> </Grid.ColumnDefinitions> <TextBlock Name="tbkRecords" Grid.Column="0" Style="{StaticResource PageTextBlock1}" Foreground="#01544A" Visibility="Visible" /> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="1"> <Grid> <Grid.RowDefinitions > <RowDefinition Height="30"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="50"/> <ColumnDefinition Width="50"/> <ColumnDefinition Width="120*"/> <ColumnDefinition Width="50"/> <ColumnDefinition Width="30"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Name="btnFirst" Text=" 首页" IsEnabled="False" Style="{StaticResource PageTextBlock2}" MouseDown="btnFirst_MouseDown"/> <TextBlock Grid.Column="1" Name="btnPrev" Text="上一页" IsEnabled="False" Style="{StaticResource PageTextBlock2}" MouseDown="btnPrev_MouseDown" /> <Grid Grid.Column="2" Name="grid" Visibility="Visible" > <TextBlock Text="第" Style="{StaticResource PageTextBlock2}" Width="15" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="0"></TextBlock> <TextBox Width="30" Name="page" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="20,0,0,0"></TextBox> <TextBlock HorizontalAlignment="Left" Width="60" VerticalAlignment="Bottom" Name="countPage" Text="页/共页" Style="{StaticResource PageTextBlock2}" Margin="55,0,0,0"></TextBlock> <Button Name="btnGO" Content="GO" Width="25" Height="20" VerticalAlignment="Center" Margin="115,0,10,0" Click="btnGO_Click"></Button> </Grid> <TextBlock Grid.Column="3" Name="btnNext" Text="下一页" IsEnabled="False" Style="{StaticResource PageTextBlock2}" MouseDown="btnNext_MouseDown" /> <TextBlock Grid.Column="4" Name="btnLast" Text="未页" IsEnabled="False" Style="{StaticResource PageTextBlock2}" MouseDown="btnLast_MouseDown"/> </Grid> </StackPanel> </Grid> </Border> </Grid>
后台分页只是根据显示的页数和每页多少条数据筛选出数据源中的数据绑定到DataGrid中:
#region 画数据 /// <summary> /// 画数据 /// </summary> private void ReadDataTable() { try { page.Text = this.pIndex.ToString(); countPage.Text = "页/共" + MaxIndex + "页"; DataTable tmpTable = new DataTable(); tmpTable = this._dt.Clone(); int first = this.pageNum * (this.pIndex - 1); first = (first > 0) ? first : 0; //如何总数量大于每页显示数量 if (this._dt.Rows.Count >= this.pageNum * this.pIndex) { for (int i = first; i < pageNum * this.pIndex; i++) tmpTable.ImportRow(this._dt.Rows[i]); } else { for (int i = first; i < this._dt.Rows.Count; i++) tmpTable.ImportRow(this._dt.Rows[i]); } this.grdList.ItemsSource = tmpTable.DefaultView; tmpTable.Dispose(); } catch { MessageBox.Show("错误"); } finally { DisplayPagingInfo(); } } #endregion
#region 初始化数据 /// <summary> /// 初始化数据 /// </summary> /// <param name="grd"></param> /// <param name="dtt"></param> /// <param name="Num"></param> public void ShowPages(DataGrid grd, DataTable dt, int Num) { if (dt == null || dt.Rows.Count == 0) { this.Visibility = Visibility.Hidden; return; } this._dt = dt.Clone(); this.grdList = grd; this.pageNum = Num; this.pIndex = 1; foreach (DataRow r in dt.Rows) this._dt.ImportRow(r); SetMaxIndex(); ReadDataTable(); } #endregion
在使用时,直接写在DataGrid下面:<my:DataGridPage x:Name="gridpage" VerticalAlignment="Bottom" Width="500"></my:DataGridPage>
后台在绑定DataGrid数据时直接使用: gridpage.ShowPages(grid1, Helper.ToDataTable(Prolist), 5);
OK,源码分享一下,希望大家多多指教!