Net-informations.com
SiteMap  | About    

C# DataGridView Paging

datagrid-paging

The DataGridView control and its related classes are designed to be a flexible, extensible system for displaying and editing tabular data. The cell is the fundamental unit of interaction for the DataGridView. All cells derive from the DataGridViewCell base class. Each cell within the DataGridView control can have its own style, such as text format, background color, foreground color, and font. If the DataGridView has lot of rows we can implement paging functionalities to the DataGridView control.

While we implement paging we should know the boundaries of the pages to enable the paging in the DatagridView. The following C# program provides a way to programatically implement paging in a Windows Datagrid View control. Here the DataGridView rows fixed as five rows and other two buttons are there for implementing paging functionalities.




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");
        }
    }
}






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.