Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C# 3.5

Listview Paging in C#

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
31 Jan 2013CPOL3 min read 53.3K   3K   8  
Listview Paging Feature to split the records of the listview in sevaral pages.
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 System.Data.OleDb;




namespace LsvPaging
{
    
    public partial class frmLsvPage : Form
    {
       public frmLsvPage()
        {
            InitializeComponent();
            
        }

       private void frmLsvPage_Load(object sender, EventArgs e)
       {
           lsvData.Width = this.Width - 30;
           lsvData.Height = pnlNavigate.Top-100;
           pnlNRPP.Left = this.Width - pnlNRPP.Width-30;
           LsvPageGlobVar.NRPP = Convert.ToInt32(nudNRPP.Value);
           
           LsvPageFunc.DbConnection();
           LsvPageGlobVar.Page = 1;
           LsvPageFunc.FillLsvData(LsvPageFunc.ExecSQLQry("Select * from tbl_Employee"),lsvData,0);
           lblInfo.Text="Record Shown: " + LsvPageGlobVar.RecStart + " to "+LsvPageGlobVar.RecEnd+ "                      Page "+LsvPageGlobVar.Page+" of " + LsvPageGlobVar.TotalPages;
       }

       private void frmLsvPage_Resize(object sender, EventArgs e)
       {
           lsvData.Width = this.Width - 30;
           lsvData.Height = pnlNavigate.Top - 100;
           pnlNRPP.Left = this.Width - pnlNRPP.Width - 30;
       }

       private void btnFirst_Click(object sender, EventArgs e)
       {
           LsvPageGlobVar.Page = 1;
           LsvPageFunc.FillLsvData(LsvPageFunc.ExecSQLQry("Select * from tbl_Employee"), lsvData, 0);
           lblInfo.Text = "Record Shown: " + LsvPageGlobVar.RecStart + " to " + LsvPageGlobVar.RecEnd + "                      Page " + LsvPageGlobVar.Page + " of " + LsvPageGlobVar.TotalPages;
       }

       private void btnLast_Click(object sender, EventArgs e)
       {
           LsvPageGlobVar.Page = LsvPageGlobVar.TotalPages;
           LsvPageFunc.FillLsvData(LsvPageFunc.ExecSQLQry("Select * from tbl_Employee"), lsvData, 0);
           lblInfo.Text = "Record Shown: " + LsvPageGlobVar.RecStart + " to " + LsvPageGlobVar.RecEnd + "                      Page " + LsvPageGlobVar.Page + " of " + LsvPageGlobVar.TotalPages;
       }

       private void btnNext_Click(object sender, EventArgs e)
       {
           if (LsvPageGlobVar.Page < LsvPageGlobVar.TotalPages)
           {
               LsvPageGlobVar.Page++;
           }
           LsvPageFunc.FillLsvData(LsvPageFunc.ExecSQLQry("Select * from tbl_Employee"), lsvData, 0);
           lblInfo.Text = "Record Shown: " + LsvPageGlobVar.RecStart + " to " + LsvPageGlobVar.RecEnd + "                      Page " + LsvPageGlobVar.Page + " of " + LsvPageGlobVar.TotalPages;
       }

       private void btnPrev_Click(object sender, EventArgs e)
       {
           if (LsvPageGlobVar.Page > 1)
           {
               LsvPageGlobVar.Page--;
           }
           LsvPageFunc.FillLsvData(LsvPageFunc.ExecSQLQry("Select * from tbl_Employee"), lsvData, 0);
           lblInfo.Text = "Record Shown: " + LsvPageGlobVar.RecStart + " to " + LsvPageGlobVar.RecEnd + "                      Page " + LsvPageGlobVar.Page + " of " + LsvPageGlobVar.TotalPages;
       }

       private void nudNRPP_ValueChanged(object sender, EventArgs e)
       {
           if (nudNRPP.Value != 0)
           {
               LsvPageGlobVar.NRPP = Convert.ToInt32(nudNRPP.Value);
           }
           else
           {
               nudNRPP.Value = 1;
           }
           LsvPageFunc.FillLsvData(LsvPageFunc.ExecSQLQry("Select * from tbl_Employee"), lsvData, 0);
           lblInfo.Text = "Record Shown: " + LsvPageGlobVar.RecStart + " to " + LsvPageGlobVar.RecEnd + "                      Page " + LsvPageGlobVar.Page + " of " + LsvPageGlobVar.TotalPages;
           
       }

       private void frmLsvPage_FormClosing(object sender, FormClosingEventArgs e)
       {
           
           MessageBox.Show("Thanks & Regards \n By Anand. G, \n ag@live.in \n This is Redirect to My Facebook Page","List View With Paging",MessageBoxButtons.OK,MessageBoxIcon.Information);
           System.Diagnostics.Process.Start("http://www.facebook.com/anandkch");
       }
        
        
    }

    public class LsvPageGlobVar
    {
        public static string ConStr;
        public static DataTable sqlDataTable = new DataTable();
        public static int TotalRec; //Variable for getting Total Records of the Table
        public static int NRPP; //Variable for Setting the Number of Recrods per listiview page
        public static int Page; //List View Page for Navigate or move
        public static int TotalPages; //Varibale for Counting Total Pages.
        public static int RecStart; //Variable for Getting Every Page Starting Record Index
        public static int RecEnd; //Variable for Getting Every Page End Record Index
    }
    public class LsvPageFunc
    {
        public static bool DbConnection()
        {
            bool functionReturnValue = false;

            try
            {
                 LsvPageGlobVar.ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=../../Database.mdb";
                //LsvPageGlobVar.ConStr ="Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=|DataDirectory|\data.mdb";      
                
                OleDbConnection sqlCon = new OleDbConnection();
                sqlCon.ConnectionString = LsvPageGlobVar.ConStr;
                sqlCon.Open();
                functionReturnValue = true;
                sqlCon.Close();
            }
            catch (Exception ex)
            {
                functionReturnValue = false;
                MessageBox.Show("Error : " + ex.ToString());
            }
            return functionReturnValue;
        }

        //Function to execute all queires
        public static DataTable ExecSQLQry(string SQLQuery)
        {
            try
            {
                OleDbConnection sqlCon = new OleDbConnection(LsvPageGlobVar.ConStr);
                OleDbDataAdapter sqlDA = new OleDbDataAdapter(SQLQuery, sqlCon);
                OleDbCommandBuilder sqlCB = new OleDbCommandBuilder(sqlDA);

                LsvPageGlobVar.sqlDataTable.Reset();
                sqlDA.Fill(LsvPageGlobVar.sqlDataTable);
            }
            catch (Exception ex)
            {

                MessageBox.Show("Error : " + ex.ToString());

            }
            return LsvPageGlobVar.sqlDataTable;
        }


        public static void FillLsvData(DataTable sqlData, ListView lvList, int imageID)
        {
            //Load the table data in the listview
            int i = 0;
            int j = 0;
            int m = 0;
            int xsize;
            

            lvList.Clear();
            // for Adding Column Names from the datatable

            LsvPageGlobVar.TotalRec = sqlData.Rows.Count;

            //try
            //{
                LsvPageGlobVar.TotalPages = LsvPageGlobVar.TotalRec / LsvPageGlobVar.NRPP;

                if (LsvPageGlobVar.TotalRec % LsvPageGlobVar.NRPP > 0)
                {
                    LsvPageGlobVar.TotalPages++;
                }
            //}

            //catch(DivideByZeroException e)
            //{
            //    MessageBox.Show("Error : " + e.ToString());
            //}
            

            for (i = 0; i <= sqlData.Columns.Count - 1; i++)
            {
                lvList.Columns.Add(sqlData.Columns[i].ColumnName);
            }

            //for adding records to the listview from datatable
            int l, k;
            
            l = (LsvPageGlobVar.Page - 1) * LsvPageGlobVar.NRPP;
            k = ((LsvPageGlobVar.Page) * LsvPageGlobVar.NRPP);
            
            LsvPageGlobVar.RecStart = l + 1;
            if (k > LsvPageGlobVar.TotalRec)
            {
                LsvPageGlobVar.RecEnd = LsvPageGlobVar.TotalRec;
            }
            else
            {
                LsvPageGlobVar.RecEnd = k;
            }

            for (; l < k; l++)
            {
                if (l >= LsvPageGlobVar.TotalRec)
                {
                    break;
                }

                lvList.Items.Add(sqlData.Rows[l][0].ToString(), imageID);
               
                for (j = 1; j <= sqlData.Columns.Count - 1; j++)
                {
                    if (!System.Convert.IsDBNull(sqlData.Rows[l][j]))
                    {
                        lvList.Items[m].SubItems.Add(sqlData.Rows[l][j].ToString());
                        
                    }
                    else
                    {
                        lvList.Items[m].SubItems.Add("");
                        
                    } 
                }
                m++;
            }


            //for rearrange the column size
            for (i = 0; i <= sqlData.Columns.Count - 1; i++)
            {
                xsize = lvList.Width / sqlData.Columns.Count - 8;

                if (xsize > 1450)
                {
                    lvList.Columns[i].Width = xsize;
                    lvList.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                }

                else
                {
                    lvList.Columns[i].Width = 2000;
                    lvList.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
                }



            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer Azeal Groups
India India
Thinking Innovation. .. ...

Technologies: .NET, Java, PHP, Python, SQL, Android, HTML, .

Comments and Discussions