分页查询操作
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
开发工具与关键技术:Visual Studio 2015 * SQL Server
作者:
撰写时间:2019年01月01日
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
不管是MVC还是WPF中都不会少分页操作按钮的使用,一般拿它来查询数据,分页操作按钮为了更快的查询出数据,
private void btn_Select_Click(object sender, RoutedEventArgs e)
{
dtStationManage = ChaXunShuJu(intPageSize, intPageIndex, false, true);
dgStationManage.ItemsSource = dtStationManage.AsDataView();
if (dtStationManage.Rows.Count < 1)
{
intPageIndex = 0;
txtCurrentPage.Text = "0";
cboPageLineCount.IsEnabled = false;
txtCurrentPage.IsEnabled = false;
}
else
{
cboPageLineCount.IsEnabled = true;
txtCurrentPage.IsEnabled = true;
}
UpdatePagingButton();
}
首页、上一页、下一页和尾页的做法,页面一加载时,首页和上一页的按钮为禁用状态,当当前页已是表格的最大页时,下一页和尾页为禁用状态。
private void imgMostUp_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (((Image)sender).Opacity == 1)
{
intPageIndex = 1;
txtCurrentPage.Text = "1";
btn_Select_Click(null, null);
}
}
private void imgUp_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (((Image)sender).Opacity == 1)
{
intPageIndex--;
txtCurrentPage.Text = intPageIndex.ToString();
btn_Select_Click(null, null);
}
}
首页,上一页和下一页、尾页写法基本一样,先定义一个点击事件,在用if判断,((Image)sender).Opacity == 1,就当前索引+1,设置文本值显示当前页,在调用查询方法。
private void cboPageLineCount_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
intPageSize = Convert.ToInt32(cboPageLineCount.SelectedValue);
intPageIndex = 1;
btn_Select_Click(null, null);
}
private void txtCurrentPage_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
TextChange[] change = new TextChange[e.Changes.Count];
e.Changes.CopyTo(change, 0);
int offset = change[0].Offset;
if (change[0].AddedLength > 0)
{
int num = 0;
if (!int.TryParse(textBox.Text, out num))
{
textBox.Text = textBox.Text.Remove(offset, change[0].AddedLength).ToString().Trim();
textBox.Select(offset, 0);
}
}
if (decPageCount != 0)
{
if (Convert.ToInt32(textBox.Text.Trim()) > decPageCount)
{
textBox.Text = decPageCount.ToString();
}
else if (Convert.ToInt32(textBox.Text.Trim()) < 1)
{
textBox.Text = "1";
}
}
if (intPageIndex != Convert.ToInt32(textBox.Text.Trim()) && Convert.ToInt32(textBox.Text.Trim()) >= 1)
{
intPageIndex = Convert.ToInt32(textBox.Text.Trim());
btn_Select_Click(null, null);
}
}