Friday, May 17, 2013

DataGridView Paging in c# WinForm


 I have a window  project in which I had to show  specific number of records and that that specific number of records  will display in each next and previous button . I have used list<> for the datasource  For Datagridview.



        int skip = 0, take = 0, nextCount=0;
  private void StoreRoomBM_Load(object sender, EventArgs e)
        {
            FillAllLoadTime();
           
        }


      void FillAllLoadTime()
        {
          
            var a = from p in lstDetails.Take(5)
                     select p;                 
            if (a.ToList().Count > 0)
            {
                int showtotal = a.ToList().Count;
                DataDetailsData.DataSource = a.ToList();
                LblREcordCount.Text = 1 + " - " + showtotal + "  of " +   lstDetails.Count;
                btnPrevious.Enabled = false;
                LblRecord.Visible = false;
            }
            else
            {

                DataDetailsData.DataSource = null;
                LblRecord.Visible = true;
            }

         

        }

private void btnNext_Click(object sender, EventArgs e)
        {
            try
            {
               
               nextCount ++;
                for (int i = 1; i < LstDetails.ToList().Count/5 + 1 ; i++)
                {
                    if (nextCount == i)
                    {
                        
                        skip = skip + 5;
                        take =  5;
                        var a = from p in LstDetails.Skip(skip).Take(take)
                                select p;
                        if (a.ToList().Count > 0)
                        {
                            btnPrevious.Enabled = true;
                            DataDetailsData.DataSource = a.ToList();
                            LblREcordCount.Text = (skip + 1) + " - " + (skip + take) + "  of " + LstDetails.Count;
                            LblRecord.Visible = false;
                        }
                        else
                        {

                            DataDetailsData.DataSource = null;
                            LblRecord.Visible = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
        }
private void btnPrevious_Click(object sender, EventArgs e)
        {
            try
            {

                nextCount--;
                for (int i = 0; i < LstDetails.ToList().Count / 5 ; i++)
                {
                    if (nextCount == i)
                    {

                        skip = skip - 5;
                        take = 5;
                        var a = from p in LstDetails.Skip(skip).Take(take)
                                select p;
                        if (a.ToList().Count > 0)
                        {
                            
                            DataDetailsData.DataSource = a.ToList();
                            LblREcordCount.Text = (skip + 1) + " - " + (skip + take) + "  of " + LstDetails.Count;
                            LblRecord.Visible = false;
                        }
                        else
                        {

                            DataDetailsData.DataSource = null;
                            LblRecord.Visible = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
        }

2 comments:

  1. i am getting "Error 6 The name 'LstDetails' does not exist in the current context"

    ReplyDelete
    Replies
    1. LstDetails is the name of the list or u can say gridview using in this scenario

      Delete