// 定义分页需要的类
public class PageArgument
{
private int total_page { get; set; }
/// <summary>
/// Current page
/// </summary>
[DataMember]
public int currentPage { get; set; }
/// <summary>
/// total count of the record
/// </summary>
[DataMember]
public int totalCount { get; set; }
/// <summary>
/// true: sorted in ascending order , false sorted in descending order
/// </summary>
[DataMember]
public bool isAsc { get; set; }
/// <summary>
/// item number by page
/// </summary>
[DataMember]
public int numPerPage { get; set; }
/// <summary>
/// number of total page
/// </summary>
[DataMember]
public int totalPage
{
get
{
return (int)Math.Ceiling((double)totalCount / numPerPage);
}
set
{
total_page = value;
}
}
}
// Code behind
1 IList<tbl_tbone_request> list = null; // 对象数据集
2 PageArgument pageArgument = new PageArgument(); //实例化 分页对象
3 DataServiceProxy dataClient = null;
4
5 public Pagination(DataServiceProxy dataClient)
6 {
7 this.dataClient = dataClient;
8 InitializeComponent();
9 InitPage();
10 }
11
12 private void InitPage()
13 {
14 //初始化分页对象值
15 pageArgument.currentPage = 1;
16 pageArgument.totalCount = 0;
17 pageArgument.numPerPage = 20;
18
19 // 邦定前段 显示当前第几页
20 Binding cBinding = new Binding();
21 cBinding.Source = pageArgument;
22 cBinding.Path = new PropertyPath("currentPage");
23 CurrentPage.SetBinding(Label.ContentProperty, cBinding);
24
25 // 邦定前段 显示页面总计
26 Binding tBinding = new Binding();
27 tBinding.Source = pageArgument;
28 tBinding.Path = new PropertyPath("totalPage");
29 TotalPage.SetBinding(Label.ContentProperty, tBinding);
30
31 // 获取邦定数据
32 BindRequestListData();
33 }
34
35
36 private void BindRequestListData()
37 {
38 //实例化对象(添加过滤条件)
39 tbl_tbone_request requester = new tbl_tbone_request();
40 //实例化临时分页对象
41 PageArgument pa = new PageArgument();
42
43 pa.currentPage = pageArgument.currentPage;
44 pa.numPerPage = pageArgument.numPerPage;
45 pa.totalCount = pageArgument.totalCount;
46
47 // 获取对象集合
48 list = dataClient.GetRequestDataByPage(requester, ref pa);
49
50 // 获得数据总页数
51 pageArgument.totalPage = pa.totalPage;
52 RequestList.ItemsSource = list;//邦定
53 }
54
55 /// <summary>
56 /// 前一页
57 /// </summary>
58 /// <param name="sender"></param>
59 /// <param name="e"></param>
60 private void PrePage_Click(object sender, RoutedEventArgs e)
61 {
62 new Thread(() =>
63 {
64 App.Current.Dispatcher.BeginInvoke(new Action(() => {
65 if (Convert.ToInt32(CurrentPage.Content)>1)
66 {
67 pageArgument.currentPage = pageArgument.currentPage - 1;
68 BindRequestListData();
69 }
70 // _loading.Visibility = Visibility.Collapsed;
71 }));
72 }).Start();
73 // _loading.Visibility = Visibility.Visible;
74 }
75
76 /// <summary>
77 /// 后一页
78 /// </summary>
79 /// <param name="sender"></param>
80 /// <param name="e"></param>
81 private void NextPage_Click(object sender, RoutedEventArgs e)
82 {
83 new Thread(() =>
84 {
85 App.Current.Dispatcher.BeginInvoke(new Action(() =>
86 {
87 if (Convert.ToInt32(CurrentPage.Content)<Convert.ToInt32(TotalPage.Content))
88 {
89 pageArgument.currentPage = pageArgument.currentPage + 1;
90 BindRequestListData();
91 }
92 // _loading.Visibility = Visibility.Collapsed;
93 }));
94 }).Start();
95 // _loading.Visibility = Visibility.Visible;
96 }
97
98 /// <summary>
99 /// 跳转具体页面
100 /// </summary>
101 /// <param name="sender"></param>
102 /// <param name="e"></param>
103 private void TransferPage_Click(object sender, RoutedEventArgs e)
104 {
105 Regex rg = new Regex(@"^[d]{0,}$");
106 if (!string.IsNullOrEmpty(SpecifiedPage.Text) && !rg.IsMatch(SpecifiedPage.Text))
107 {
108 MessageBox.Show("Please input number!");
109 return;
110 }
111
112 new Thread(() =>
113 {
114 App.Current.Dispatcher.BeginInvoke( new Action(()=>{
115 if (!string.IsNullOrEmpty(SpecifiedPage.Text) && Convert.ToInt32(SpecifiedPage.Text)>=1 && Convert.ToInt32(SpecifiedPage.Text) <= Convert.ToInt32(TotalPage.Content))
116 {
117 pageArgument.currentPage = Convert.ToInt32(SpecifiedPage.Text);
118 BindRequestListData();
119 }
120 // _loading.Visibility = Visibility.Collapsed;
121 }));
122 }).Start();
123 // _loading.Visibility = Visibility.Visible;
124 }
// 前段页面
1 <DataGrid HorizontalAlignment="Center" Grid.Row="2" VerticalAlignment="Top" x:Name="RequestList" AutoGenerateColumns="False" SelectionMode="Single" CanUserAddRows="False" Height="407" >
2 <DataGrid.Columns>
3 <DataGridTextColumn Header="Requester ID" Binding="{Binding Path=Req_ID}"></DataGridTextColumn>
4 <DataGridTextColumn Header="Request ID" Binding="{Binding Path=Req_ID}" Width="75" IsReadOnly="True" />
5 <DataGridTextColumn Header="Requester" Binding="{Binding Path=Requester}" Width="80" IsReadOnly="True"/>
6 <DataGridTextColumn Header="System" Binding="{Binding Path=Prod_Type}" Width="60" IsReadOnly="True"/>
7 <DataGridTextColumn Header="Region" Binding="{Binding Path=Region}" Width="60" IsReadOnly="True"/>
8 <DataGridTextColumn Header="Country" Binding="{Binding Path=Country}" Width="60" IsReadOnly="True"/>
9 <DataGridTextColumn Header="Environment" Binding="{Binding Path=Env}" Width="80" IsReadOnly="True"/>
10 <DataGridTextColumn Header="Created" Binding="{Binding Path=Created}" Width="140" IsReadOnly="True"/>
11 <DataGridTextColumn Header="Last Changed" Binding="{Binding Path=Last_Changed_TS}" Width="140" IsReadOnly="True"/>
12 <DataGridTextColumn Header="Status" Binding="{Binding Path=Req_Status}" Width="60" IsReadOnly="True"/>
13 <DataGridTextColumn Header="Completion" Binding="{Binding Path=Completion_TS}" Width="140" IsReadOnly="True"/>
14 <DataGridTextColumn Header="Multiple batches indicator" Binding="{Binding Path=Regression_Num}" Width="150" IsReadOnly="True"/>
15 <DataGridTextColumn Header="Current batch indicator" Binding="{Binding Path=Current_Regression}" Width="140" IsReadOnly="True"/>
16 </DataGrid.Columns>
17 </DataGrid>
18 <StackPanel Orientation="Horizontal" Grid.Row="3" HorizontalAlignment="Center" >
19 <Button Content="Previous" Height="28" HorizontalAlignment="Left" Name="btnPrepage" VerticalAlignment="Top" Width="65" Click="PrePage_Click" />
20 <Button Content="Next" Height="28" HorizontalAlignment="Left" Margin="5,0,0,0" Name="btnNextPage" VerticalAlignment="Top" Width="59" Click="NextPage_Click" />
21 <TextBox Height="23" HorizontalAlignment="Left" Name="SpecifiedPage" VerticalAlignment="Top" Width="53" Margin="5,0,0,0"/>
22 <Button Content="GO" Height="28" HorizontalAlignment="Left" Name="btnTranPage" Margin="5,0,0,0" VerticalAlignment="Top" Width="96" Click="TransferPage_Click" />
23 <Label Content="Current Page:" Height="28" HorizontalAlignment="Left" Name="lblCurrPage" VerticalAlignment="Top"/>
24 <Label Height="28" HorizontalAlignment="Left" Name="CurrentPage" VerticalAlignment="Top" />
25 <!--<Label Content="页】" Height="28" HorizontalAlignment="Left" Name="label10" VerticalAlignment="Top" />-->
26 <Label Content="Total Page:" Height="28" HorizontalAlignment="Left" Name="lblTotalPage" VerticalAlignment="Top" />
27 <Label Height="28" HorizontalAlignment="Left" Name="TotalPage" VerticalAlignment="Top" />
28 </StackPanel>
/// WcfService method
1 public IList<tbl_tbone_request> GetRequestDataByPage(tbl_tbone_request request, ref PageArgument pageArgument)
2 {
3 IList<tbl_tbone_request> list = null;
4 using (TBONE_G2CEntities dbContext = new TBONE_G2CEntities())
5 {
6 try
7 {
8 //
9 int skipNum = pageArgument.numPerPage * (pageArgument.currentPage - 1);
10 ParameterExpression pe = Expression.Parameter(typeof(tbl_tbone_request), "req");
11 Type[] types = new Type[] { dbContext.tbl_tbone_request.AsQueryable<tbl_tbone_request>().ElementType };
12 Expression originalExpression = dbContext.tbl_tbone_request.AsQueryable<tbl_tbone_request>().Expression;
13 BinaryExpression eq = null;
14
15 // Add where condition, 根据是否有值,动态添加过滤条件
//如果 Requester不为空,就添加这个过滤条件
16 if (!string.IsNullOrEmpty(request.Requester))
17 {
18 MemberExpression left = Expression.Property(pe, "Requester");
19 ConstantExpression right = Expression.Constant(request.Requester);
20 eq = eq == null ? Expression.Equal(left, right) : Expression.AndAlso(eq, Expression.Equal(left, right));
21 }
// 如果 Req_id 有值,就加这个过滤条件
if (request.Req_ID > 0)
{
MemberExpression left = Expression.Property(pe, "Req_ID");
ConstantExpression right = Expression.Constant(request.Req_ID);
eq = eq == null ? Expression.Equal(left, right) : Expression.AndAlso(eq, Expression.Equal(left, right));
}
22
23 LambdaExpression lambada = eq == null ? null : Expression.Lambda<Func<tbl_tbone_request, bool>>(eq, pe);
24 MethodCallExpression WhereCondition = lambada == null ? null : Expression.Call(typeof(Queryable), "Where", types, originalExpression, lambada);
25 var querySql = WhereCondition == null ? dbContext.tbl_tbone_request.OrderByDescending(t => t.Req_ID) : dbContext.tbl_tbone_request.AsQueryable<tbl_tbone_request>().Provider.CreateQuery<tbl_tbone_request>(WhereCondition).OrderByDescending(t => t.Req_ID).AsQueryable<tbl_tbone_request>();
26 int TotalNum = querySql.Count();
27 pageArgument.totalCount = TotalNum;
28 querySql = querySql.Skip(skipNum).Take(pageArgument.numPerPage);
29 list = querySql.ToList<tbl_tbone_request>();
// Add where condition, 根据是否有值,动态添加过滤条件
30 } 31 catch (Exception) 32 { 33 } 34 return list; 35 } 36 37 }