分页技术

效果图:

 

<Window x:Class="Ch12Sample_03.Window4"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window4" Height="307" Width="454" FontSize="14" >
    <Grid>
        <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">
            <DataGrid.Columns>
                <DataGridTextColumn Header="编号" Binding="{Binding Path=title_id}" />
                <DataGridTextColumn Header="书名" Binding="{Binding Path=title}" />
                <DataGridTextColumn Header="价格" Binding="{Binding Path=price}" />
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="上一页" Height="23" HorizontalAlignment="Left" Margin="37,185,0,0" Name="button7" VerticalAlignment="Top" Width="65" Click="button7_Click" />
        <Button Content="下一页" Height="23" HorizontalAlignment="Left" Margin="108,185,0,0" Name="button8" VerticalAlignment="Top" Width="59" Click="button8_Click" />
        <Label Content="转到" Height="28" HorizontalAlignment="Left" Margin="173,186,0,0" Name="label6" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="212,187,0,0" Name="textBox1" VerticalAlignment="Top" Width="53" />
        <Button Content="GO" Height="23" HorizontalAlignment="Left" Margin="298,185,0,0" Name="button9" VerticalAlignment="Top" Width="96" Click="button9_Click" />
        <Label Content="" Height="28" HorizontalAlignment="Left" Margin="269,185,0,0" Name="label7" VerticalAlignment="Top" />
        <Label Height="28" HorizontalAlignment="Left" Margin="120,228,0,0" Name="label8" VerticalAlignment="Top" Content="【第" />
        <Label Content="1" Height="28" HorizontalAlignment="Left" Margin="157,228,0,0" Name="label1" VerticalAlignment="Top" />
        <Label Content="页】" Height="28" HorizontalAlignment="Left" Margin="176,228,0,0" Name="label10" VerticalAlignment="Top" />
        <Label Content="【共" Height="28" HorizontalAlignment="Left" Margin="242,228,0,0" Name="label11" VerticalAlignment="Top" />
        <Label Content="1" Height="28" HorizontalAlignment="Left" Margin="280,228,0,0" Name="label2" VerticalAlignment="Top" />
        <Label Content="页】" Height="28" HorizontalAlignment="Left" Margin="298,228,0,0" Name="label13" VerticalAlignment="Top" />
    </Grid>
</Window>
前台代码:
namespace Ch12Sample_03
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class Window4 : Window
    {

        public Window4()
        {
            InitializeComponent();
            DataGridBind(Convert.ToInt32(label1.Content));
            //label11.Content的初值为1,即显示第1页
        }


        //每页显示5条记录
        public const int pageSize = 5;


        private void DataGridBind(int pageIndex)
        {

            //pageIndex 当前页的序号
            int recordsum=0;   //表中记录总数
            int PageSum=0;     //总页数         
            
            PubsDataContext pubs = new PubsDataContext();

            var query = from title in pubs.titles
                        select title;

            recordsum = query.Count();

            //判断总页数
            if (recordsum % pageSize == 0)
                PageSum = recordsum / pageSize;
            else
                PageSum = recordsum / pageSize + 1;


            dataGrid1.ItemsSource = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);


            label1.Content = Convert.ToString(pageIndex);
            label2.Content = Convert.ToString(PageSum);
        }


        private void button7_Click(object sender, RoutedEventArgs e)
        {
            //上一页
            if (Convert.ToInt32(label1.Content) > 1)
                DataGridBind(Convert.ToInt32(label1.Content)-1);

        }

        private void button8_Click(object sender, RoutedEventArgs e)
        {
            //下一页
            if (Convert.ToInt32(label1.Content) < Convert.ToInt32(label2.Content))
                DataGridBind(Convert.ToInt32(label1.Content) + 1);
        }


        private void button9_Click(object sender, RoutedEventArgs e)
        {
            //调转到第n页
            if(Convert.ToInt32(textBox1.Text)>=1 && Convert.ToInt32(textBox1.Text)<=Convert.ToInt32(label2.Content))
                DataGridBind(Convert.ToInt32(textBox1.Text));
            
        }

    }
}
后台代码:

 

posted @ 2016-09-13 21:47  白色的回忆  阅读(186)  评论(0编辑  收藏  举报