Search This Blog

Wednesday, October 19, 2011

DataGridView Paging In Windows Application

  public partial class Form1 : Form
    {

        protected SqlConnection mcnSample;

        private int mintTotalRecords = 0;
        private int mintPageSize = 0;
        private int mintPageCount = 0;
        private int mintCurrentPage = 1;
        DataSet ds = new DataSet();
        protected const string CONNECTION_STRING = @"Data Source=ASHOK-PC\SQLEXPRESS;Initial Catalog=Demo4;Persist Security Info=True;User ID=ashok;Password=1234";

        public Form1()
        {
            InitializeComponent();
        }
        private void fillGrid()
        { 
            this.mintPageSize = int.Parse(this.tbPageSize.Text == "" ? "5" : this.tbPageSize.Text);
            this.mintTotalRecords = ds.Tables[0].Rows.Count;
            this.mintPageCount = this.mintTotalRecords / this.mintPageSize;
            if (this.mintTotalRecords % this.mintPageSize > 0)
                this.mintPageCount++;
            this.mintCurrentPage = 0;
            loadPage();
        }
        private void loadPage()
        {
            string strSql = "";
            int intSkip = 0;
            intSkip = (this.mintCurrentPage * this.mintPageSize);
            if (ds.Tables.Count < 1)
            {
                strSql = "SELECT " +
                    " PK_ID,Name,City,Qualification,Category FROM Product";

                SqlCommand cmd = this.mcnSample.CreateCommand();
                cmd.CommandText = strSql;

                SqlDataAdapter da = new SqlDataAdapter(cmd);

                da.Fill(ds, "Product");

                cmd.Dispose();
                da.Dispose();
            }
            mintPageSize = mintPageSize == 0 ? 5 : mintPageSize;
            var a = ds.Tables[0].AsEnumerable().Skip(mintPageSize * mintCurrentPage).Take(mintPageSize);
            this.dataGridView1.DataSource = a.CopyToDataTable();
            this.lblStatus.Text = (this.mintCurrentPage + 1).ToString() +
              " / " + this.mintPageCount.ToString();
        }
        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();
        }
       
        private void btnPreviois_Click(object sender, EventArgs e)
        {
            this.goPrevious();
        }
        private void btnfirst_Click(object sender, EventArgs e)
        {
            this.goFirst();
        }
        private void btnnext_Click(object sender, EventArgs e)
        {
            this.goNext();
        }
        private void btnlast_Click(object sender, EventArgs e)
        {
            this.goLast();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Show();
            this.openConnection();
            tbPageSize.Text = "5";
            loadPage();
            fillGrid();
        }
        private void openConnection()
        {
            try
            {
                this.mcnSample = new SqlConnection(CONNECTION_STRING);
                this.mcnSample.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void closeConnection()
        {
            try
            {
                if (this.mcnSample.State == ConnectionState.Open)
                    this.mcnSample.Close();
                this.mcnSample.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void tbPageSize_KeyUp(object sender, KeyEventArgs e)
        {
            this.fillGrid();
           
        }