Calculate the number of pages based on total number of records and page size
Add those many buttons to a container like StackPanel and
On those buttons click events bind the DataGrid to desired range of records.
1.Define the following Properties and Variables
public partial class Page : UserControl
{
//Property to hold the value of total records
public int TotalRow { get; set; }
//Property to hold the value of page size
public int PageSize { get; set; }
//Number of rows that the DataGrid
int totalRowsInGrid = 0;
2. Initialize the values public Page()
{
InitializeComponent();
//Initialize the page size
PageSize = 10;
//Get the desired data
user = User.Get();
//Set the TotalRow cout to the number of rows returned for data
TotalRow = user.Count;
BindGrid(1);
DoPaging(this.PageSize, TotalRow);
}
3. Create a method to get the desired range of data based on page number///
/// Returns paged data based on page number
///
///
///
private List
{
int Pagestart = (pageNumber - 1) * this.PageSize;
TextBlockStartPageNumber.Text = (Pagestart+1).ToString();
TextBlockEndPageNumber.Text = (Pagestart + totalRowsInGrid).ToString();
TextBlockTotalRecords.Text = TotalRow.ToString();
int i = TotalRow;
List
//List
//List
if ((i - ((pageNumber - 1) * this.PageSize)) >= this.PageSize)
PagedData = user.GetRange(Pagestart, this.PageSize);
else
PagedData = user.GetRange(Pagestart, (i % this.PageSize));
return PagedData;
}
4. Bind the DataGrid///
/// Bind grid to the desired range of records
///
///
private void BindGrid(int PageNumber)
{
if (this.PageSize * PageNumber > this.TotalRow)
{
totalRowsInGrid = this.TotalRow % this.PageSize;
}
else
{
totalRowsInGrid = this.PageSize;
}
dataGridPaging.ItemsSource = GetData(PageNumber);
}
5. Write the click event for the paging buttons///
/// Handles the click event of buttons having page numbers
///
///
///
void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
int iPageNumber = Convert.ToInt32(btn.Content.ToString());
BindGrid(iPageNumber);
}
Regard
Prateek
can you please explain this with snapshots.
ReplyDelete