效果圖:
前台代碼:
1 <Window x:Class="Ch12Sample_03.Window4" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="Window4" Height="307" Width="454" FontSize="14" > 5 <Grid> 6 <DataGrid AutoGenerateColumns="False" Height="148" HorizontalAlignment="Left" Margin="27,21,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="380" IsReadOnly="True" CanUserAddRows="False" VerticalScrollBarVisibility="Auto" SelectionMode="Single" EnableRowVirtualization="False"> 7 <DataGrid.Columns> 8 <DataGridTextColumn Header="編號" Binding="{Binding Path=title_id}" /> 9 <DataGridTextColumn Header="書名" Binding="{Binding Path=title}" /> 10 <DataGridTextColumn Header="價格" Binding="{Binding Path=price}" /> 11 </DataGrid.Columns> 12 </DataGrid> 13 <Button Content="上一頁" Height="23" HorizontalAlignment="Left" Margin="37,185,0,0" Name="button7" VerticalAlignment="Top" Width="65" Click="button7_Click" /> 14 <Button Content="下一頁" Height="23" HorizontalAlignment="Left" Margin="108,185,0,0" Name="button8" VerticalAlignment="Top" Width="59" Click="button8_Click" /> 15 <Label Content="轉到" Height="28" HorizontalAlignment="Left" Margin="173,186,0,0" Name="label6" VerticalAlignment="Top" /> 16 <TextBox Height="23" HorizontalAlignment="Left" Margin="212,187,0,0" Name="textBox1" VerticalAlignment="Top" Width="53" /> 17 <Button Content="GO" Height="23" HorizontalAlignment="Left" Margin="298,185,0,0" Name="button9" VerticalAlignment="Top" Width="96" Click="button9_Click" /> 18 <Label Content="頁" Height="28" HorizontalAlignment="Left" Margin="269,185,0,0" Name="label7" VerticalAlignment="Top" /> 19 <Label Height="28" HorizontalAlignment="Left" Margin="120,228,0,0" Name="label8" VerticalAlignment="Top" Content="【第" /> 20 <Label Content="1" Height="28" HorizontalAlignment="Left" Margin="157,228,0,0" Name="label1" VerticalAlignment="Top" /> 21 <Label Content="頁】" Height="28" HorizontalAlignment="Left" Margin="176,228,0,0" Name="label10" VerticalAlignment="Top" /> 22 <Label Content="【共" Height="28" HorizontalAlignment="Left" Margin="242,228,0,0" Name="label11" VerticalAlignment="Top" /> 23 <Label Content="1" Height="28" HorizontalAlignment="Left" Margin="280,228,0,0" Name="label2" VerticalAlignment="Top" /> 24 <Label Content="頁】" Height="28" HorizontalAlignment="Left" Margin="298,228,0,0" Name="label13" VerticalAlignment="Top" /> 25 </Grid> 26 </Window>
后台代碼:
1 namespace Ch12Sample_03 2 { 3 /// <summary> 4 /// MainWindow.xaml 的交互邏輯 5 /// </summary> 6 public partial class Window4 : Window 7 { 8 9 public Window4() 10 { 11 InitializeComponent(); 12 DataGridBind(Convert.ToInt32(label1.Content)); 13 //label11.Content的初值為1,即顯示第1頁 14 } 15 16 17 //每頁顯示5條記錄 18 public const int pageSize = 5; 19 20 21 private void DataGridBind(int pageIndex) 22 { 23 24 //pageIndex 當前頁的序號 25 int recordsum=0; //表中記錄總數 26 int PageSum=0; //總頁數 27 28 PubsDataContext pubs = new PubsDataContext(); 29 30 var query = from title in pubs.titles 31 select title; 32 33 recordsum = query.Count(); 34 35 //判斷總頁數 36 if (recordsum % pageSize == 0) 37 PageSum = recordsum / pageSize; 38 else 39 PageSum = recordsum / pageSize + 1; 40 41 42 dataGrid1.ItemsSource = query.Skip((pageIndex - 1) * pageSize).Take(pageSize); 43 44 45 label1.Content = Convert.ToString(pageIndex); 46 label2.Content = Convert.ToString(PageSum); 47 } 48 49 50 private void button7_Click(object sender, RoutedEventArgs e) 51 { 52 //上一頁 53 if (Convert.ToInt32(label1.Content) > 1) 54 DataGridBind(Convert.ToInt32(label1.Content)-1); 55 56 } 57 58 private void button8_Click(object sender, RoutedEventArgs e) 59 { 60 //下一頁 61 if (Convert.ToInt32(label1.Content) < Convert.ToInt32(label2.Content)) 62 DataGridBind(Convert.ToInt32(label1.Content) + 1); 63 } 64 65 66 private void button9_Click(object sender, RoutedEventArgs e) 67 { 68 //調轉到第n頁 69 if(Convert.ToInt32(textBox1.Text)>=1 && Convert.ToInt32(textBox1.Text)<=Convert.ToInt32(label2.Content)) 70 DataGridBind(Convert.ToInt32(textBox1.Text)); 71 72 } 73 74 } 75 }