搜索和展示都没什么可说的吧,很基础的东西~其实在listbox里点了按钮进行翻页也不难啦。。但还是写在这里记录一下吧,希望对小小白起到帮助哦
代码里的注释还算清楚,语句也没有复杂的地方,所以就直接贴代码吧
/// <summary>
/// customDataPage.xaml 的交互逻辑
/// </summary>
public partial class customDataPage : UserControl, INotifyPropertyChanged
{
private DataTable _dataGridItemsSource = new DataTable();
/// <summary>
/// DataGrid的数据源
/// </summary>
public DataTable DataGridItemsSource
{
get { return _dataGridItemsSource; }
set
{
_dataGridItemsSource = value;
ChangeDataGridItemsSource();
}
}
public static readonly DependencyProperty NumbersPerPageProperty =
DependencyProperty.Register("NumbersPerPage",
typeof(int), typeof(customDataPage), new FrameworkPropertyMetadata(10, null));
/// <summary>
/// 每页显示的最大条数
/// </summary>
public int NumbersPerPage
{
get
{
return (int)this.GetValue(NumbersPerPageProperty);
}
set
{
this.SetValue(NumbersPerPageProperty, value);
}
}
public static readonly RoutedEvent SelectionChangedEvent =
EventManager.RegisterRoutedEvent("SelectionChanged",
RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<object>),
typeof(customDataPage));
public event RoutedPropertyChangedEventHandler<object> SelectionChanged
{
add
{
this.AddHandler(SelectionChangedEvent, value);
}
remove
{
this.RemoveHandler(SelectionChangedEvent, value);
}
}
public void OnSelectionChanged(object oldValue, object newValue)
{
RoutedPropertyChangedEventArgs<object> arg =
new RoutedPropertyChangedEventArgs<object>
(oldValue, newValue, SelectionChangedEvent);
this.RaiseEvent(arg);
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
/// <summary>当前页码</summary>
private int _currentPage = 1;
public int CurrentPage
{
get { return _currentPage; }
set
{
_currentPage = value;
//数值改变时通知前台
OnPropertyChanged("CurrentPage");
}
}
/// <summary>总页数</summary>
private int _totalPages = 1;
public int TotalPages
{
get { return _totalPages; }
set
{
_totalPages = value;
//数值改变时通知前台
OnPropertyChanged("TotalPages");
}
}
private DataTable currentDataTable = new DataTable();
public customDataPage()
{
InitializeComponent();
}
private void LayoutRoot_Loaded_1(object sender, RoutedEventArgs e)
{
this.tbIndex.DataContext = this;
this.tbTotal.DataContext = this;
//SetDatatable();
currentDataTable = DataGridItemsSource;
if ((DataGridItemsSource != null) && (DataGridItemsSource.Rows.Count != 0))
{
SetDataGrid(currentDataTable);
TotalPages = DataGridItemsSource.Rows.Count / NumbersPerPage;
if (DataGridItemsSource.Rows.Count % NumbersPerPage != 0)
TotalPages++;
if (TotalPages > 1)
{
SetPageButtons();
}
DisplayPageInfo();
}
else
{
//无数据时清空 add by yxd 0503
dg.ItemsSource = null;
TotalPages = 1;
SetPageButtons();
}
dg.AddHandler(DataGrid.SelectionChangedEvent, new RoutedEventHandler(OnSelectionChanged), true);
}
/// <summary>
/// 设置DataGrid要显示的数据
/// </summary>
private void SetDataGrid(DataTable currentTable)
{
dg.ItemsSource = null;
int restNums = 0;
if (currentDataTable.Rows.Count - NumbersPerPage * (CurrentPage - 1) < NumbersPerPage)
restNums = currentDataTable.Rows.Count;
else
restNums = NumbersPerPage * CurrentPage;
if (currentTable.Rows.Count >= NumbersPerPage)
{
DataTable tempTable = new DataTable();
tempTable = currentTable.Clone();
for (int i = NumbersPerPage * (CurrentPage - 1);
i < restNums;
i++)
{
tempTable.ImportRow(currentTable.Rows[i]);
}
dg.ItemsSource = tempTable.DefaultView;
tempTable.Dispose();
}
else
dg.ItemsSource = currentTable.DefaultView;
}
/// <summary>
/// 设置显示页数的按钮
/// </summary>
private void SetPageButtons()
{
List<int> lstPage = new List<int>();
for (int i = 0; i < TotalPages; i++)
{
lstPage.Add(i + 1);
}
lbPageNumbers.ItemsSource = lstPage;
}
/// <summary>
/// 控制各按钮是否可用
/// </summary>
private void DisplayPageInfo()
{
if (DataGridItemsSource.Rows.Count <= NumbersPerPage
|| DataGridItemsSource == null)
{
btnPrev.IsEnabled = false;
btnNext.IsEnabled = false;
btnGo.IsEnabled = false;
tbGoNum.IsEnabled = false;
return;
}
if (CurrentPage == 1)
btnPrev.IsEnabled = false;
else
btnPrev.IsEnabled = true;
if (CurrentPage == TotalPages)
btnNext.IsEnabled = false;
else
btnNext.IsEnabled = true;
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrev_Click_1(object sender, RoutedEventArgs e)
{
CurrentPage--;
SetDataGrid(currentDataTable);
DisplayPageInfo();
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNext_Click_1(object sender, RoutedEventArgs e)
{
CurrentPage++;
SetDataGrid(currentDataTable);
DisplayPageInfo();
}
/// <summary>
/// 跳转
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGo_Click_1(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
CurrentPage = int.Parse(tbGoNum.Text.Trim());
SetDataGrid(currentDataTable);
DisplayPageInfo();
tbGoNum.Text = string.Empty;
btnGo.IsEnabled = false;
}
/// <summary>
/// 跳转到第几页,文本变化时控制跳转按钮的可用状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tbGoNum_TextChanged_1(object sender, TextChangedEventArgs e)
{
int pageNum = 0;
if (int.TryParse(tbGoNum.Text.Trim(), out pageNum))
{
if (pageNum <= TotalPages)
btnGo.IsEnabled = true;
}
else
return;
}
private void SetDatatable()
{
DataGridItemsSource.Columns.Add("dh", typeof(string));
DataGridItemsSource.Columns.Add("zh", typeof(string));
for (int i = 0; i < 99; i++)
{
DataRow dr = DataGridItemsSource.NewRow();
dr["dh"] = "00" + i.ToString();
dr["zh"] = "123" + i.ToString();
DataGridItemsSource.Rows.Add(dr);
}
}
/// <summary>
/// 搜索按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSearch_Click_1(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(tbZh.Text.Trim()))
return;
DataTable temp = new DataTable();
temp = (dg.ItemsSource as DataView).Table.Clone();
DataRow[] rows = DataGridItemsSource.Select("Bind_BH='" + tbZh.Text.Trim() + "'");
if (rows.Count() != 0)
{
for (int i = 0; i < rows.Count(); i++)
{
temp.ImportRow(rows[i]);
}
currentDataTable = temp;
SetDataGrid(currentDataTable);
}
DisplayPageInfo();
}
/// <summary>
/// 获取全部按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCheckAll_Click_1(object sender, RoutedEventArgs e)
{
currentDataTable = DataGridItemsSource;
SetDataGrid(currentDataTable);
DisplayPageInfo();
}
int prevIndex = 0;
/// <summary>
/// 点击页数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lbPageNumbers_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
if (lbPageNumbers.SelectedItem == null)
return;
int currentIndex = int.Parse(lbPageNumbers.SelectedItem.ToString());
if (currentIndex > prevIndex)
{
if (currentIndex <= lbPageNumbers.Items.Count - 3)
{
lbPageNumbers.ScrollIntoView(lbPageNumbers.Items[currentIndex + 2]);
}
else if (currentIndex <= lbPageNumbers.Items.Count - 2)
{
lbPageNumbers.ScrollIntoView(lbPageNumbers.Items[currentIndex + 1]);
}
}
else if (currentIndex < prevIndex)
{
if (currentIndex - 2 >= 0)
{
lbPageNumbers.ScrollIntoView(lbPageNumbers.Items[currentIndex - 2]);
}
else if (currentIndex - 1 >= 0)
{
lbPageNumbers.ScrollIntoView(lbPageNumbers.Items[currentIndex - 1]);
}
}
CurrentPage = currentIndex + 1;
prevIndex = currentIndex;
SetDataGrid(currentDataTable);
DisplayPageInfo();
}
/// <summary>
/// 刷新DataGrid数据源
/// </summary>
private void ChangeDataGridItemsSource()
{
currentDataTable = DataGridItemsSource;
if (DataGridItemsSource != null)
{
SetDataGrid(currentDataTable);
TotalPages = DataGridItemsSource.Rows.Count / NumbersPerPage;
if (DataGridItemsSource.Rows.Count % NumbersPerPage != 0)
TotalPages++;
if (TotalPages > 1)
{
SetPageButtons();
}
DisplayPageInfo();
}
}
}