Paging for DataGridView Control in Windows C# Application


By default Datagridview doesn't have Pagination. Following Code snippet helps to create a DataGridView User Control with Pagination.

Follow the following steps to Create the DataGridView User Conrtol With Pagination

1. Create Windows C# Application
2. Add a New User Control and Name it as CustomDatGrid.cs.
3. Drag and Drop a DataGridView COntrol on above newly created user Control and name it as dgvRecords.
4. Now add 4 buttons and a label to the User Control for paging.
a)Button -> btnFirst
b)Button -> btnPrevious
c)Button -> btnNext
d)Button -> btnLast
e)Label -> lblRecordsStatus
Align the control as You wish.
Please find the attached Source code with design for mOre information.
5. In the Code behind of User Control define following properties and a static variable to track the current page number.


// Data Table as a source to the DataGridView Control
private DataTable dtSource = new DataTable();

// To hold the current page no
static int currentPageIndex = 0;

// Page Size of the GridView
private int pageSize = 5;

public int PageSize
{
set{
this.pageSize =value;
}
get{
return this.pageSize;
}
}
public DataTable DataSource
{
set
{
this.dtSource = value;
if (dtSource != null)
{
if (dtSource.Rows.Count == 0 || dtSource.Rows.Count <= pageSize)
{
// Make controls Enabled property to false if no data binded to the gridview
btnPrevious.Enabled = false;
btnNext.Enabled = false;
btnLast.Enabled = false;
btnFirst.Enabled = false;
lblRecordsStatus.Text = "0 / 0";
}
}
}
get
{
return this.dtSource;
}
}

6. Now define a method to bind the data to the grid view.

// Method to bind the data to the gridview
public void BindGrid()
{
//if data source contains records
if (dtSource.Rows.Count > 0)
{
//Create a temporray table
DataTable tmptable = dtSource.Clone();

//set the start index
int startIndex = currentPageIndex * pageSize;

//set the last index
int endIndex = currentPageIndex * pageSize + pageSize;

// if end index value geater than datasource records count set the end index as datasource rows count.
if (endIndex > dtSource.Rows.Count)
{
endIndex = dtSource.Rows.Count;
}

// Get the records from start index to end index and add it to the above created temp data table.
for (int i = startIndex; i < endIndex; i++)
{
DataRow newRow = tmptable.NewRow();
// GetNewRow will get the datarow ata a particular index from datasource.
GetNewRow(ref newRow, dtSource.Rows[i]);
tmptable.Rows.Add(newRow);
}
dgvRecords.DataSource = tmptable;

// Assign text to the Record status
lblRecordsStatus.Text = (currentPageIndex + 1) + " / " + (int)Math.Ceiling(Convert.ToDecimal(dtSource.Rows.Count) / pageSize);
}

// To Make the gridview columns as read only.
MakeGridColumnsReadOnly();
}

private void GetNewRow(ref DataRow newRow, DataRow source)
{
foreach (DataColumn col in dtSource.Columns)
{
newRow[col.ColumnName] = source[col.ColumnName];
}
}

// To Make the GridView columns read only.
private void MakeGridColumnsReadOnly()
{
for (int i = 0; i < dgvRecords.Columns.Count; i++)
{
dgvRecords.Columns[i].ReadOnly = true;
}
}

7. Now add LCick events to the above added Four Buttons. set the Current page index and Make the buttons enbaled property to true or false.

private void btnFirst_Click(object sender, EventArgs e)
{
Cursor currentCursor = this.Cursor;
this.Cursor = Cursors.WaitCursor;
splitContainer1.Enabled = false;
currentPageIndex = 0;
btnPrevious.Enabled = false;
btnNext.Enabled = true;
btnLast.Enabled = true;
btnFirst.Enabled = false;
BindGrid();
splitContainer1.Enabled = true;
this.Cursor = currentCursor;
}

private void btnLast_Click(object sender, EventArgs e)
{
Cursor currentCursor = this.Cursor;
this.Cursor = Cursors.WaitCursor;
splitContainer1.Enabled = false;
currentPageIndex = (int)Math.Ceiling(Convert.ToDecimal(dtSource.Rows.Count) / pageSize)-1;
btnFirst.Enabled = true;
btnPrevious.Enabled = true;
btnNext.Enabled = false;
btnLast.Enabled = false;
BindGrid();
this.Cursor = currentCursor;
splitContainer1.Enabled = true;
}

private void btnNext_Click(object sender, EventArgs e)
{
Cursor currentCursor = this.Cursor;
this.Cursor = Cursors.WaitCursor;
splitContainer1.Enabled = false;
currentPageIndex++;
if (currentPageIndex == (int)Math.Ceiling(Convert.ToDecimal(dtSource.Rows.Count) / pageSize)-1)
{
btnNext.Enabled = false;
btnLast.Enabled = false;
}
else
{
btnNext.Enabled = true;
btnLast.Enabled = true;

}
btnFirst.Enabled = true;
btnPrevious.Enabled = true;

BindGrid();
this.Cursor = currentCursor;
splitContainer1.Enabled = true;
}

private void btnPrevious_Click(object sender, EventArgs e)
{
Cursor currentCursor = this.Cursor;
this.Cursor = Cursors.WaitCursor;
splitContainer1.Enabled = false;
currentPageIndex--;
if (currentPageIndex == 0)
{
btnPrevious.Enabled = false;
btnFirst.Enabled = false;
}
else
{
btnPrevious.Enabled = true;
btnFirst.Enabled = true;

}
btnNext.Enabled = true;
btnLast.Enabled = true;
BindGrid();
this.Cursor = currentCursor;
splitContainer1.Enabled = true;
}


7. Now Build the application
8. Now add a Windows form and drag and drop the User Control and name it as customDataGrid1.
9. Binding the data to the above added User control

DataTable data = new DataTable();
//data= get data from data base to buind
customDataGrid1.PageSize = 10;
customDataGrid1.DataSource = data;
customDataGrid1.BindGrid();

Advantages
1. User Control can be used in any Windows form applications.
2. Page Size can be customizable.

Disadvantages
1.We can provide only datatable as datasource.

Plese find the attached CustomDataGrid.cs user contro source Code and Screen shot of the Design


Attachments

  • SourceCode (38423-1964-CustomDataGrid.zip)
  • Related Articles

    Date Time Windows Form User Control

    User Control to display the current date & time on Windows form. Delagates, Timer and Evnets are used to display the current date and time.

    More articles: GridView Paging User control Pagination user control

    Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: