Building equal number of records list object I am trying to get equal number of records in both columns, even if that means that first column doesn't fill page to bottom on the second page. The only way I was able to do this was by using a maxrow count and breaking when it is equal to the max row count.

 public List&lt;DataForExcelExport&gt; GetData()
        {

            var list = new List&lt;DataForExcelExport&gt;();
            int ROWS_PER_PAGE = 41;
            int MaxRow = 0;

            SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCommandText, connectionString);
            DataTable dataTable = new DataTable();
            dataAdapter.Fill(dataTable);
            bool flgLastPage = false;
            for (int i = 0; i &lt; dataTable.Rows.Count; i++)
            {
                DataRow dr = dataTable.Rows[i];
                var item = new DataForExcelExport();
                SetItemData(dr, item, true);
                if (i + ROWS_PER_PAGE &lt; dataTable.Rows.Count)
                {
                    SetItemData(dataTable.Rows[i + ROWS_PER_PAGE], item, false);
                }
                list.Add(item);
                if (flgLastPage == false)
                {
                    if ((i + 1) % ROWS_PER_PAGE == 0)
                    {
                        i += ROWS_PER_PAGE;

                        int remaining = dataTable.Rows.Count - (i + 1);
                        if (remaining &lt; 2 * ROWS_PER_PAGE)
                        {
                            ROWS_PER_PAGE = remaining / 2 + remaining % 2;
                            flgLastPage = true;

                        }
                    }
                }
                int maxrow = i + ROWS_PER_PAGE;
                if (maxrow == dataTable.Rows.Count)
                {
                    break;

                }
            }
            return list;

        }


expected output: when there are a total of 118 rows

- page 1 
_________
  1 | 41
... |...
 38 | 78
 39 | 79
 40 | 80
_________

- page 2
_________
 81 | 99
 82 | 100
... | ...
98  |118
_________
Try following some simple rules: Avoid setting flags in loops. Try calculating the data you need before taking action. Only increment counters inside loop declaration. This way even if something gets broken - you will not have to wonder "how did we end up here?". Instead you will be able to tell "this index / limit is not calculated correctly".

Also when saying 


  even if that means that first column doesn't fill page to bottom on the second page


Did you mean:


  even if that means both columns don't fill the last page to the bottom


This is how I understood it.

int ROWS_PER_PAGE = 41;
int totalDataCount = dataTable.Rows.Count;
int pagesToProcess = (int)Math.Ceiling(totalDataCount / 2.0 / ROWS_PER_PAGE);
int itemsPerPage = ROWS_PER_PAGE * 2;

for(int pageIdx = 0; pageIdx &lt; pagesToProcess; pageIdx++)
{
    int itemsOnThisPage = (pageIdx + 1) * itemsPerPage &gt;= totalDataCount ? totalDataCount - pageIdx * itemsPerPage : itemsPerPage;
    int rowsOnThisPage = (int)Math.Ceiling( itemsOnThisPage / 2.0 );
    for( int rowIdx = 0; rowIdx &lt; rowsOnThisPage; rowIdx++)
    {
        var item = new DataForExcelExport();
        int itemIdx1 = pageIdx * itemsPerPage + rowIdx;
        int itemIdx2 = itemIdx1 + rowsOnThisPage;
        SetItemData(dataTable.Rows[itemIdx1], item, true);
        if(totalDataCount &gt; itemIdx2)
            SetItemData(dataTable.Rows[itemIdx2], item, false);
    }
}


Ah yes, and try to name variables in a meaningful way. Names like i or maxrow do not tell what the variable is really used for.