接着上一篇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 = ; 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 = ; i < values.Length; i++)
{
values[i] = properties[i].GetValue(item);
}
dt.Rows.Add(values);
}
return dt;
}
废话不多说,先上一个分页的效果图:
分页布局代码如下:
<Grid>
<Border CornerRadius="" Background="Transparent" BorderBrush="#01544A" BorderThickness="">
<Grid HorizontalAlignment="Stretch" Margin="5 0 1 0" VerticalAlignment="Top" Width="Auto" Height="">
<Grid.ColumnDefinitions>
<ColumnDefinition Width=""/>
<ColumnDefinition Width="300*" MinWidth=""/>
</Grid.ColumnDefinitions>
<TextBlock Name="tbkRecords" Grid.Column="" Style="{StaticResource PageTextBlock1}" Foreground="#01544A" Visibility="Visible" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="">
<Grid>
<Grid.RowDefinitions >
<RowDefinition Height=""></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=""/>
<ColumnDefinition Width=""/>
<ColumnDefinition Width="120*"/>
<ColumnDefinition Width=""/>
<ColumnDefinition Width=""/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="" Name="btnFirst" Text=" 首页" IsEnabled="False" Style="{StaticResource PageTextBlock2}" MouseDown="btnFirst_MouseDown"/>
<TextBlock Grid.Column="" Name="btnPrev" Text="上一页" IsEnabled="False" Style="{StaticResource PageTextBlock2}" MouseDown="btnPrev_MouseDown" />
<Grid Grid.Column="" Name="grid" Visibility="Visible" >
<TextBlock Text="第" Style="{StaticResource PageTextBlock2}" Width="" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin=""></TextBlock>
<TextBox Width="" Name="page" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="20,0,0,0"></TextBox>
<TextBlock HorizontalAlignment="Left" Width="" VerticalAlignment="Bottom" Name="countPage" Text="页/共页" Style="{StaticResource PageTextBlock2}" Margin="55,0,0,0"></TextBlock>
<Button Name="btnGO" Content="GO" Width="" Height="" VerticalAlignment="Center" Margin="115,0,10,0" Click="btnGO_Click"></Button>
</Grid>
<TextBlock Grid.Column="" Name="btnNext" Text="下一页" IsEnabled="False" Style="{StaticResource PageTextBlock2}" MouseDown="btnNext_MouseDown" />
<TextBlock Grid.Column="" 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 - );
first = (first > ) ? first : ;
//如何总数量大于每页显示数量
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 == )
{
this.Visibility = Visibility.Hidden;
return;
}
this._dt = dt.Clone(); this.grdList = grd;
this.pageNum = Num;
this.pIndex = ;
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,源码分享一下,希望大家多多指教!