Home > DeveloperSection > Forums > How to add paging in datagridview in C#
marcel ethan
marcel ethan

Total Post:105

Points:735
Posted on    June-15-2013 4:32 AM

 C# C# 
Ratings:


 1 Reply(s)
 858  View(s)
Rate this:
Hi Developers,

How add paging concept in datagridview using c#

Any help will be appreciateable.



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    June-15-2013 7:49 AM

Hi Marcel,

below I provide you some line of code in datagridview.

using System;

using System.Data;

using System.Windows.Forms;

using System.Data.SqlClient;

using System.Drawing;

 

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        SqlDataAdapter pagingAdapter;

        DataSet pagingDS;

        int scrollVal;

 

        public Form1()

        {

            InitializeComponent();

            scrollVal = 0;

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True";

            string sql = "SELECT * FROM Authors";

            SqlConnection connection = new SqlConnection(connectionString);

            pagingAdapter = new SqlDataAdapter(sql, connection);

            pagingDS = new DataSet();

            connection.Open();

            pagingAdapter.Fill(pagingDS, scrollVal, 5, "Authors_table");

            connection.Close();

            dataGridView1.DataSource = pagingDS;

            dataGridView1.DataMember = "Authors_table";

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            scrollVal = scrollVal - 5;

            if (scrollVal < = 0)

            {

                scrollVal = 0;

            }

            pagingDS.Clear();

            pagingAdapter.Fill(pagingDS, scrollVal, 5, "authors_table");

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            scrollVal = scrollVal + 5;

            if (scrollVal > 23)

            {

                scrollVal = 18;

            }

            pagingDS.Clear();

            pagingAdapter.Fill(pagingDS, scrollVal, 5, "authors_table");

        }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick