Search code examples
c#datatabledatagridviewpagination

Paging - To list previous set of values form datatable in c#


I hold a temp datatable called temptable which holds about 22 rows and it has been displayed in datagrid in 5 records in each page consider I'm in 3rd page where ther will b only 2 records now if i click on previous button i need to display previous set of (15-20) 5 values and again if i click on previous it should display (10 - 15) 5 values and so on ..

I have pasted my code here.. problem here is i could only retrieve only first 5 previous values i mean 15 - 20 .. i need to make it generic but couldnt get idea out of it

   private void PreviousSelect_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
      {
        TempTable.Clear();

  /* if Total_Temp_Table_rowCount is 22 int a  will be ' 2 ' and
     int b will return 20 */

        int  a = Total_Temp_Table_rowCount % 5;

        int  b = Total_Temp_Table_rowCount- a;

        for (int s = b-1 ; s >= b - 5; s--)
        {
            fieldLabel = (string)selectedFieldsTable.Rows[s]["Field Name"].ToString();
            fieldType = (string)selectedFieldsTable.Rows[s]["Field Type"].ToString();

            DataRow newRows = TempTable.NewRow();
            newRows["Field Name"] = fieldLabel;
            newRows["Field Type"] = fieldType;
            TempTable.Rows.Add(newRows);
            Console.WriteLine(selectedFieldsTable.Rows[s]["Field Name"].ToString());  
        }          
        dgvSelectedFieldsView.DataSource = TempTable;
    }

Can any one help me out pls Thanks !!


Solution

  • though you could have used DataPager but, if you are implementing it manually, you need to consider few things.

    • PageSize
    • CurrentPage
    • TotalPages

    from these only you can track current set of records to be populated in the dataGridView.

    See my logic below: you might need to fine tune the loops start and end point. but it should give you an idea.

    public int currentPage =0;
    public int totalPages=0;
    public int pageSize=5;
    public DataTable tempDt = new DataTable();
    
    public void LoadGrid()
    {
        //calculate the total number of pages..
        double result = (double)selectedFieldsTable.Rows.Count/pageSize;
        if(result>(selectedFieldsTable.Rows.Count/pageSize))
            ++result;
        totalPages = result;
    
    
             foreach (DataColumn col in selectedFieldsTable.Columns)
             {
                tempDt.Columns.Add(col);
             }
            BindGrid();
    }
    

    call LoadGrid() in the Page_Load event of the Page

    here is your previous LinkButton event

    private void PreviousSelect_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        currentPage--;
       if(currentPage<0)
       {
            (sender as LinkButton).Enabled=false;
       }    
       else
       {
            BindGrid();
        }
    }
    

    and here is your next Link Button event

    private void NextSelect_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        currentPage++;
       if(currentPage>=PageSize)
       {
            (sender as LinkButton).Enabled=false;
       }    
       else
       {
            BindGrid();
        }
    }
    

    and here is your BindGrid()

    pubilc void BindGrid()
    {
        tempDt.Clear();
        for(int i=(currentPage*pageSize);i<(currentPage*pageSize)+pageSize; i++)
        {
            DataRow row =  tempDt.NewRow();
           foreach(DataColumn col in tempDt.Columns)
           {
              row[col] = selectedFieldsTable.Rows[i][col];
           } 
           tempDt.Rows.Add(row); 
        }
        dgvSelectedFieldsView.DataSource = tempDt;   
    }
    

    CurrentPage * PageSize gives you the start record number and CurrentPage*PageSize + PageSize gives you last record number. so you insert all the included rows and bind to datagrid. test it..and see if it works for you, I'am away from a dev machine.:)