Click here to Skip to main content
14,485,408 members
Rate this:
Please Sign up or sign in to vote.
See more:
Below is the code for `DataGridView` pagination (which I copied from some other site). I have implemented this in my application (C# win form), which is going to be used by my client and this `DataGridView` will eventually have to load up thousands of records.

I need to make sure if this `DataGridView` could handle all those records. It will be loading 500 records at once by using pagination. As I don't have the records to test it now, could someone experienced point me out if this is okay or not?

Code :

public partial class GridView : Form
    {
        private int rowIndex = 0;
        private int totalRecords = 0;
        private int mintTotalRecords = 0;
        private int mintPageSize = 0;
        private int mintPageCount = 0;
        private int mintCurrentPage = 1;
        public Form11()
        {
            InitializeComponent();
        }

        private void fillGrid()
        {
            try
            {
                this.mintPageSize = 500; //page size
                this.mintTotalRecords = getCount();
                this.mintPageCount = this.mintTotalRecords / this.mintPageSize;
                if (this.mintTotalRecords % this.mintPageSize > 0)
                    this.mintPageCount++;
                this.mintCurrentPage = 0;
                loadPage();
            }
            catch (Exception ex)
            {
            }
        }

        private int getCount()
        {
            SqlConnection con = new SqlConnection();
            try
            {
                con.ConnectionString = "//My Connection String";
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandText = "getTotalNo";
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.Clear();
                con.Open();
                SqlDataReader dr = com.ExecuteReader();
                while (dr.Read())
                {
                    totalRecords = Convert.ToInt32(dr["total"].ToString());
                }
            }
            catch (Exception ex)
            {
                totalRecords = 0;
            }
            return totalRecords;
        }

        public void loadPage()
        {
            SqlConnection con = new SqlConnection();
            try
            {
                int intSkip = 0;
                intSkip = (this.mintCurrentPage * this.mintPageSize);
                con.ConnectionString = "//My Connection String";
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandText = "showRecord";
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.Clear();
                com.Parameters.AddWithValue("@pagesize", mintPageSize.ToString());
                com.Parameters.AddWithValue("@skip", intSkip.ToString());
                con.Open();
                SqlDataReader dr = com.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(dr);
                dgRecords.DataSource = dt;
                label1.Text = (this.mintCurrentPage + 1).ToString() + " / " + this.mintPageCount.ToString();
            }
            catch (Exception ex)
            {
            }
        }

        private void loadbtns()
        {
            DataGridViewButtonColumn cell = new DataGridViewButtonColumn(); 
            cell.HeaderText = "View Details";
            cell.Name = "View";
            cell.Visible = true;
            cell.Width = 100;
            cell.Text = "View Details";
            cell.UseColumnTextForButtonValue = true;

            dgRecords.Columns.Add(cell);
        }

        private void lnkFirst_Click(object sender, EventArgs e)
        {
            try
            {
                this.mintCurrentPage = this.mintPageCount - 1;
                loadPage();
            }
            catch
            {
            }
        }

        private void lnkNext_Click(object sender, EventArgs e)
        {
            try
            {
                this.mintCurrentPage++;
                if (this.mintCurrentPage > (this.mintPageCount - 1))
                    this.mintCurrentPage = this.mintPageCount - 1;
                loadPage();
            }
            catch
            {
            }
        }

        private void lnkPrevious_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.mintCurrentPage == this.mintPageCount)
                    this.mintCurrentPage = this.mintPageCount - 1;
                this.mintCurrentPage--;
                if (this.mintCurrentPage < 1)
                    this.mintCurrentPage = 0;
                loadPage();
            }
            catch
            {
            }
        }

        private void lnkLast_Click(object sender, EventArgs e)
        {
            try
            {
                this.mintCurrentPage = 0;
                loadPage();
            }
            catch
            {
            }
        }

        private void GridView_Load(object sender, EventArgs e)
        {
            fillGrid();
            loadbtns();
        }


Stored Procedure "getTotalNo"

CREATE PROCEDURE getTotalNo
    as
    begin
	select count(*) total
	from table
	end


Stored Procedure "showRecord"

CREATE PROCEDURE showRecord
    @pagesize int,
    @skip int
    as
    begin
    SELECT TOP (@pagesize) * FROM table WHERE ID NOT IN (SELECT TOP (@Skip) ID FROM table)
    end
Posted
Updated 21-Apr-14 2:29am
v3
Comments
Sergey Alexandrovich Kryukov 21-Apr-14 11:18am
   
Please see my comment to Solution 1. You are doing wrong thing. Why would we even look at code you copied who-knows-where?
—SA

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

If you simply cut and paste code off the web you will not learn as quickly as you would if you learn to understand the methods behind the code. I think that is more important than having others evaluate the code for you. There will be a will a day where you will need to debug code and you can’t simply post the code to have others debug it for you.

Since you are admittedly a “new” programmer it would benefit you to learn how to connect to and use databases. (Trust me, you will eventually get there at some point in your career anyway.)

There are some sample databases provided by Microsoft for programmers to practice on. Here is a tutorial on installing and using the sample databases.
How to: Install Sample Databases[^]

I would suggest going through the entire tutorial.
Working with Sample Databases[^]


Good luck.
   
Comments
Sergey Alexandrovich Kryukov 21-Apr-14 11:17am
   
Good points, my 5.

I think we always should deny looking at the code copied by inquirers who-knows-where. This is counter-productive. We should review only the code written by inquirers themselves.

—SA
S Houghtelin 21-Apr-14 11:20am
   
Thank you Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100