Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to fill datagridview in c#
Posted

C#
private void FillGrid()
       {
           // Set the start and max records.
           pageSize = 3;
           maxRec = dtSource.Rows.Count;
           PageCount = maxRec / pageSize;
           //  Adjust the page number if the last page contains a partial page.

           if (((maxRec % pageSize) > 0))
           {
               PageCount = (PageCount + 1);
           }
           // Initial seeings
           currentPage = 1;
           recNo = 0;
           //  Display the content of the current page.
           LoadPage();
       }

        private void LoadPage()
        {
            int i;
            int startRec;
            int endRec;
            DataTable dtTemp;
            // Duplicate or clone the source table to create the temporary table.
            dtTemp = dtSource.Clone();
            if ((currentPage == PageCount))
            {
                endRec = maxRec;
            }
            else
            {
                endRec = (pageSize * currentPage);
            }
            startRec = recNo;
            if ((dtSource.Rows.Count > 0))
            {
                // Copy the rows from the source table to fill the temporary table.
                for (i = startRec; (i <= (endRec - 1)); i++)
                {
                    dtTemp.ImportRow(dtSource.Rows[i]);
                    recNo = (recNo + 1);
                }
            }
            dataGridView1.DataSource = dtTemp;
            //DisplayPageInfo();
        }
 
Share this answer
 
there are 2 ways :-
1) By Looping
2) By Setting Datasource

which one do you want?
 
Share this answer
 

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