Visual Studio .NET - How to paging a datagridview in winform?

Asked By Manivannan G on 22-Dec-08 06:45 AM
Hi all!
 How to paging a datagridview in windows application in number format?


try this link

ram kumar replied to Manivannan G on 22-Dec-08 06:48 AM

Hi,

http://www.codeproject.com/useritems/DataGridView_manipulation.asp


rams

Reply

Manivannan G replied to ram kumar on 22-Dec-08 06:49 AM
Hi Ramkumar!

Thanks for your immediate reply. I need a paging like 1 2 3 4 ......

http://www.eggheadcafe.com/community/aspnet/2/10009945/paging-a-windows-forms-da.aspx

Web Star replied to Manivannan G on 22-Dec-08 06:51 AM
end of post
try this
ram kumar replied to Manivannan G on 22-Dec-08 06:57 AM

Hi Mani,

in that example

you change the button into label and make it as number input

and create the label (dynamically) depending upon the record count.

you got a way but u do some modification



Rams

paging in datagridview
Venkat K replied to Manivannan G on 22-Dec-08 06:58 AM

here we are processing the records through select queries in backend:

In this example, the loadPage() method fetches only the required page data from the table using the following select statement.

// Select only the n records.
strSql = "SELECT TOP " + this.mintPageSize + 
    " * FROM tblEmp WHERE E_Id NOT IN " + 
    "(SELECT TOP " + intSkip + " E_Id FROM tblEmp)";
// Protected Connection.
protected SqlConnection mcnSample;
// Page
private int mintTotalRecords = 0;
private int mintPageSize = 0;
private int mintPageCount = 0;
private int mintCurrentPage = 1;
// Connection String
protected const string CONNECTION_STRING = Server=localhost;UID=sa;PWD=;Database=Sample";
fillGrid() method:
private void fillGrid()
{
	// For Page view.
	this.mintPageSize = int.Parse(this.tbPageSize.Text);
	this.mintTotalRecords = getCount();
	this.mintPageCount = this.mintTotalRecords / this.mintPageSize;
	// Adjust page count if the last page contains partial page.
	if (this.mintTotalRecords % this.mintPageSize > 0)
		this.mintPageCount++;
	this.mintCurrentPage = 0;
	loadPage();
}
getCount() method: This method gets the record count much faster than SELECT COUNT(*) statement.
private int getCount()
{
    // This select statement is very fast compare to SELECT COUNT(*)
    string strSql = "SELECT Rows FROM SYSINDEXES WHERE Id = OBJECT_ID('tblEmp') AND IndId < 2";
    int intCount = 0;
    SqlCommand cmd = this.mcnSample.CreateCommand();
    cmd.CommandText = strSql;
    intCount = (int) cmd.ExecuteScalar();
    cmd.Dispose();
    return intCount;
}
loadPage() method:
 
private void loadPage()
{
    string strSql = "";
    int intSkip = 0;
    intSkip = (this.mintCurrentPage * this.mintPageSize);
    // Select only the n records.
    strSql = "SELECT TOP " + this.mintPageSize + 
        " * FROM tblEmp WHERE E_Id NOT IN " + 
        "(SELECT TOP " + intSkip + " E_Id FROM tblEmp)";
    SqlCommand cmd = this.mcnSample.CreateCommand();
    cmd.CommandText = strSql;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds, "tblEmp");
    // Populate Data Grid
    this.dgEmp.DataSource = ds.Tables["tblEmp"].DefaultView;
    // Show Status
    this.lblStatus.Text = (this.mintCurrentPage + 1).ToString() + " / " + this.mintPageCount.ToString();
    cmd.Dispose();
    da.Dispose();
    ds.Dispose();
}
For page navigation:
 
private void goFirst()
{
    this.mintCurrentPage = 0;
    loadPage();
}
private void goPrevious()
{
    if (this.mintCurrentPage == this.mintPageCount)
        this.mintCurrentPage = this.mintPageCount - 1;
    this.mintCurrentPage--;
    if (this.mintCurrentPage < 1) 
        this.mintCurrentPage = 0;
    loadPage();
}
private void goNext()
{
    this.mintCurrentPage++;
    if (this.mintCurrentPage > (this.mintPageCount - 1))
        this.mintCurrentPage = this.mintPageCount - 1;
    loadPage();
}
private void goLast()
{
    this.mintCurrentPage = this.mintPageCount - 1;
    loadPage();
}

 

reply
Binny ch replied to Manivannan G on 22-Dec-08 11:14 AM

See this link:

http://www.codeproject.com/KB/miscctrl/Pagable_DatagridView.aspx

http://www.codeproject.com/KB/database/DataGridView_manipulation.aspx

 

Re :: How to paging in datagridview in Winform
Shailendrasinh Parmar replied to Manivannan G on 23-Dec-08 06:34 AM

The idea is to use the Fill() method from dataAdapter. You can use the overloading version of Fill() method that accepts startRecord and maxRecords parameters.

// c# .net 2.0

private int currentRowsLoaded = 0;
private readonly int maxRecords = 100;

private void fillMyDataSet(bool firstLoad) {
   if (firstLoad) {
      myDataSet["myTable"].Clear();
      currentRowsLoaded = 0;
   }
   sqlDataAdapter.Fill(myDataSet, currentRowsLoaded, maxRecords, "myTable");
   currentRowsLoaded = myDataSet.Tables["myTable"].Rows.Count;
   // you don't have to put this code when filling dataSet
   // set a dataSource only need once, it's just for example.
   dataGridView1.DataSource = myDataSet.Tables["myTable"].DefaultView;
}

' vb .net 2.0

Private currentRowsLoaded as Integer = 0
Private ReadOnly maxRecords as Integer = 100

Private Sub fillMyDataSet(firstLoad As Boolean)
   If FirstLoad Then
      myDataSet.Tables.Item("myTable").Clear()
      currentRowsLoaded = 0
   End If
   sqlDataAdapter.Fill(myDataSet, currentRowsLoaded, maxRecords, "myTable")
   currentRowsLoaded = myDataSet.Tables.Item("myTable").Rows.Count
   ' you don't have to put this code when filling dataSet
   ' set a dataSource only need once, it's just for example.
   dataGridView1.DataSource = myDataSet.Tables.Item("myTable").DefaultView
End Sub

So we save the rows that have been loaded into a variable called currentRowsLoaded, next time if we want to fill again, it will start from the last currentRowsLoaded value that have been assigned.

Hope this helps.

TRY THIS LINK
C_A P replied to Manivannan G on 23-Dec-08 06:41 AM

http://www.codeproject.com/KB/database/DataGridView_manipulation.aspx

http://69.10.233.10/KB/miscctrl/Pagable_DatagridView.aspx