Sunday, 2 November 2014

pagging in datagridview in window application in c# .net

Introduction


I have recently worked on paging in DataGridView for a Window Form application. I Googled it, but generally found very complicated codes. So I wanted to write it to be easily understood by everyone. After some modifications, I tried to make my code very simple and want to share it with others.




I have used button controls for the purpose of navigating switches.
I added required variables such as PgSize for number of rows per page, CurrentPageIndex for current page index and Totalpage for calculating maximum number of pages DataGridView is able to display.


private int PgSize = 20; 
private int CurrentPageIndex = 1;
private int TotalPage=0;
As the name suggests, the following method is used to calculate the total number of pages for gridview:
private void CalculateTotalPages()
{
int rowCount = ds.Tables["Customers"].Rows.Count;
TotalPage = rowCount / PgSize;
// if any row left after calculated pages, add one more page
if (rowCount % PgSize > 0)
TotalPage += 1;
}
In order to retrieve records to display for the navigated page, I used a DataTable returned fromGetCurrentRecord() method as datasource for gridview. This method returns the top 20 rows, (Size ofPgSize variable) if navigated page is one. If navigated page is greater than one, then it calculates the previouspageOffset through the following line of code:
int PreviousPageOffSet= (page - 1) * PgSize; 
Now the query work starts, to select only pages from the current page and leaving earlier records apply query available in the following code:
private DataTable GetCurrentRecords(int page, SqlConnection con)
{
DataTable dt = new DataTable();

if (page == 1)
{
cmd2 = new SqlCommand("Select TOP " + PgSize +
" * from Customers ORDER BY CustomerID", con);
}
else
{
int PreviousPageOffSet= (page - 1) * PgSize;

cmd2 = new SqlCommand("Select TOP " + PgSize +
" * from Customers WHERE CustomerID NOT IN " +
"(Select TOP " + PreviousPageOffSet+
" CustomerID from customers ORDER BY CustomerID) ", con);
}
try
{
// con.Open();
this.adp1.SelectCommand = cmd2;
this.adp1.Fill(dt);
}
finally
{
con.Close();
}
return dt;
}
Navigating buttons are as usual. Here we only calculate the navigating pageIndex and pass it toGetCurrentRecords() method as argument to retrieve dataSource for our datagridview.
private void btnFirstPage_Click(object sender, EventArgs e)
{
this.CurrentPageIndex = 1;
this.dataGridView1.DataSource = GetCurrentRecords(this.CurrentPageIndex, con);
}

private void btnNxtPage_Click(object sender, EventArgs e)
{
if (this.CurrentPageIndex < this.TotalPage)
{
this.CurrentPageIndex++;
this.dataGridView1.DataSource =
GetCurrentRecords(this.CurrentPageIndex, con);
}
}

private void btnPrevPage_Click(object sender, EventArgs e)
{
if (this.CurrentPageIndex > 1)
{
this.CurrentPageIndex--;
this.dataGridView1.DataSource =
GetCurrentRecords(this.CurrentPageIndex, con);
}
}

private void btnLastPage_Click(object sender, EventArgs e)
{
this.CurrentPageIndex = TotalPage;
this.dataGridView1.DataSource = GetCurrentRecords(this.CurrentPageIndex, con);
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

I hope this is the easiest way for paging a DataGridView.
This is my first article I have ever posted and I have tried my best to explain my code.

No comments:

Post a Comment

Working with 3- Tier Architecture in C#

  Introduction In this article we will learn Use to 3- Tier architecture in C#.NET application. 3-Tier architecture is very famous and ...