Friday, 5 September 2014

Paging in DataGridview Using C#.Net In Windows Application

9/05/2014 - By Pranav Singh 8

In my previous article I have show you How to Bind xml to DataGridView Using C#.Net In Windows Application and Bind And Display Image in a DatagridView Using C#.Net in Windows Application. Now in this article I will show you how you can bind a datageidview control and perform paging operation in for the record in datagridview by using datatable, access database. You can use SQL databse to bind the data.


So for this article first we will create a new windows application and add a datagridview control and two button control for displaying paging functionality.


Now we will create table and add some record in it.


Now we will first write code to bind the gridview control.

public string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=bookstore.mdb;Persist Security Info=False;";
        DataTable _objdt;
        OleDbDataAdapter _objda;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            BindData();
        }
        public void BindData()
        {
            _objdt = new DataTable();
            DataTable _objtotalRecord = new DataTable();
            string querystring = "select Name,Address,Marks from Student;";
            OleDbConnection _objcon = new OleDbConnection(connectionstring);
            _objda = new OleDbDataAdapter(querystring, _objcon);
            _objcon.Open();
            //for filling the grid
            _objda.Fill(minValue, maxValue, _objdt);
            dataGridView1.DataSource = _objdt;
        }


Now we will write code on next and previous button click.

Code for NEXT button
  private void button2_Click(object sender, EventArgs e)
        {
            int nextCount = minValue + _objdt.Rows.Count;
            _objdt = new DataTable();
            _objda.Fill(nextCount, maxValue, _objdt);
            dataGridView1.DataSource = _objdt;
            minValue = nextCount;
            //Enable disable button
            if (_objdt.Rows.Count == maxValue)
            {
                button1.Enabled = true;
                button2.Enabled = true;
            }
            else
            {
                button1.Enabled = true;
                button2.Enabled = false;
                minValue = nextCount + _objdt.Rows.Count;
            }
        }

In above code first I have check the index of current record and as per value I have made button enable disable.

Code for PREVIOUS button
private void button1_Click(object sender, EventArgs e)
        {
            int nextCount = (minValue - (_objdt.Rows.Count + maxValue)) < maxValue ? 0 : (minValue - (_objdt.Rows.Count + maxValue));
            _objdt = new DataTable();
            _objda.Fill(nextCount, maxValue, _objdt);
            dataGridView1.DataSource = _objdt;
            minValue = nextCount;
            //Enable disable button
            if (minValue == 0)
            {
                button1.Enabled = false;
                button2.Enabled = true;
            }
            else
            {
                button1.Enabled = true;
                button2.Enabled = true;
            }
        }

In above code I have checked weather the final value in less the 0 or not if it’s less than 0 then make the button enable disable as per value, and pass the starting count of value to the adapter.

Now we will check the complete code of the form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.FileIO;
using System.Data.OleDb;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=bookstore.mdb;Persist Security Info=False;";
        DataTable _objdt;
        int minValue = 0;
        int maxValue = 3;
        OleDbDataAdapter _objda;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            BindData();
        }
        public void BindData()
        {
            _objdt = new DataTable();
            DataTable _objtotalRecord = new DataTable();
            string querystring = "select Name,Address,Marks from Student;";
            OleDbConnection _objcon = new OleDbConnection(connectionstring);
            _objda = new OleDbDataAdapter(querystring, _objcon);
            _objcon.Open();
            //for filling the grid
            _objda.Fill(minValue, maxValue, _objdt);
            dataGridView1.DataSource = _objdt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int nextCount = minValue + _objdt.Rows.Count;
            _objdt = new DataTable();
            _objda.Fill(nextCount, maxValue, _objdt);
            dataGridView1.DataSource = _objdt;
            minValue = nextCount;
            //Enable disable button
            if (_objdt.Rows.Count == maxValue)
            {
                button1.Enabled = true;
                button2.Enabled = true;
            }
            else
            {
                button1.Enabled = true;
                button2.Enabled = false;
                minValue = nextCount + _objdt.Rows.Count;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int nextCount = (minValue - (_objdt.Rows.Count + maxValue)) < maxValue ? 0 : (minValue - (_objdt.Rows.Count + maxValue));
            _objdt = new DataTable();
            _objda.Fill(nextCount, maxValue, _objdt);
            dataGridView1.DataSource = _objdt;
            minValue = nextCount;
            //Enable disable button
            if (minValue == 0)
            {
                button1.Enabled = false;
                button2.Enabled = true;
            }
            else
            {
                button1.Enabled = true;
                button2.Enabled = true;
            }
        }

    }
}

Now we have done run the application to check the final output.

DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

8 comments:

  1. it's working and thank you

    ReplyDelete
  2. Hi.. thanks for the nice example., but there is a problem when clicking to previous page,
    in my case, i use the maxValue = 10. when I click on previous page, the result gets back 20 records.
    can you please investigate if there is a typo or i am doing the typo..

    ReplyDelete
    Replies
    1. Hi please check the below mention line
      int nextCount = (minValue - (_objdt.Rows.Count + maxValue)) < maxValue ? 0 : (minValue - (_objdt.Rows.Count + maxValue));

      in this it may happen you have made mistake. if this is file please post your code for verification.

      Delete
    2. Dear Mr. Admin.. thanks for your quick reply..
      I checked the code, and it is the same. I will paste my code below.

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Data.SqlServerCe;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      using WHWindowsForms.Model;

      namespace WHWindowsForms
      {
      public partial class frmTest2 : Form
      {
      SqlCeConnection con2 = ConAndData.Con;
      DataTable dtTable;
      int minValue = 0;
      int maxValue = 10;
      SqlCeDataAdapter adp;

      public frmTest2()
      {
      InitializeComponent();
      }
      private void frmTest2_Load(object sender, EventArgs e)
      {
      BindData();
      }

      private void BindData()
      {
      Cursor.Current = Cursors.WaitCursor;

      dtTable = new DataTable();
      adp = new SqlCeDataAdapter("Select * from tblOrder order by OrderId Desc", con2);
      con2.Open();

      adp.Fill(minValue,maxValue, dtTable);
      dataGridView1.DataSource = dtTable;

      Cursor.Current = Cursors.Default;
      }

      private void btnNext_Click(object sender, EventArgs e)
      {
      int nextCount = minValue + dtTable.Rows.Count;
      dtTable = new DataTable();
      adp.Fill(nextCount, maxValue, dtTable);
      dataGridView1.DataSource = dtTable;
      minValue = nextCount;
      //Enable disable button
      if (dtTable.Rows.Count == maxValue)
      {
      btnPrevious.Enabled = true;
      btnNext.Enabled = true;
      }
      else
      {
      btnPrevious.Enabled = true;
      btnNext.Enabled = false;
      minValue = nextCount + dtTable.Rows.Count;
      }
      }

      private void btnPrevious_Click(object sender, EventArgs e)
      {
      int nextCount = (minValue - (dtTable.Rows.Count + maxValue)) < maxValue ? 0 : (minValue - (dtTable.Rows.Count + maxValue));
      dtTable = new DataTable();
      adp.Fill(nextCount, maxValue, dtTable);
      dataGridView1.DataSource = dtTable;
      minValue = nextCount;

      //Enable disable button
      if (minValue == 0)
      {
      btnPrevious.Enabled = false;
      btnNext.Enabled = true;
      }
      else
      {
      btnNext.Enabled = true;
      btnPrevious.Enabled = true;
      }
      }
      }
      }

      Delete
    3. Dear Admin..
      still haven't gor any reply from you..! and the code is still givving me the wrong number when clicking on previous..!!

      Delete
    4. Hi sorry for late reply. I have not found any problem in above solution. I am also using the solution in one of my project its woking fine.
      I will suggest u to.please check the data which u r using.

      But still i will check and try to.provide more optimised solution.

      Delete
  3. If it's of any use, I have resolved the problem using the following code:
    int nextCount = (minValue - dataTable.Rows.Count) < maxValue ? 0 : (minValue - dataTable.Rows.Count);

    ReplyDelete
  4. Tks for the example!! It is working now!!

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top