Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a DataGridView which is using BindingNavigator for pagination.
Here is the sample code for binding the grid
C#
//Pagination code
                    BindingNavigator1.BindingSource = BindingSource1;
                    BindingSource1.CurrentChanged += BindingNavigator1_CurrentChanged;
                    BindingSource1.DataSource = new PageOffsetList
                    {
                        TotalRecords = myDataTable.Rows.Count, //The dataTable
                        PageSize = pageSize
                    };

//Grid binding
private void BindingNavigator1_CurrentChanged(object sender, EventArgs e)
        {
            // The desired page has changed, so fetch the page of records using the "Current" offset 
            if (myDataTable!= null)
            {
                var offset = (int)BindingSource1.Current;
                var dataTable = myDataTable.Clone();
                dataTable.Rows.Clear();
                totalRecords = myDataTable.Rows.Count;

                for (int i = offset; i < offset + pageSize && i < totalRecords; i++)
                {
                    dataTable.ImportRow(myDataTable.Rows[i]);

                }
                
                BindingSource bs = new BindingSource();
                bs.DataSource = dataTable.DefaultView;
                DataView1.DataSource = bs;
                
                
            }
        }


What I have tried:

1) Now I can see the pagination working. The sorting is also working within a single page when the column header is clicked but does not affect other pages. How can I make the sorting work for all the pages?

2) For Filtering I am using DataGridViewAutoFilter.dll . With this the filtering also works but only for a single page. How to make it work for all the pages.

Sample code here.
C#
// Add the AutoFilter header cell to each column.
                var col = ValidateGrid.Columns["Column1"];
                col.HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);

                col = ValidateGrid.Columns["Column2"];
                col.HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);


I guess the sorting and filtering will work fine, if I remove the binding navigator but pagination is a requirement for me. Please suggest any solutions.
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900