views:

22

answers:

1
//Bind data
this.IncomeGridView.DataSource = incomeData;
//If incomeData is not empty then Taking the value of the Id field of the first and the last row in data page
if (incomeData.Count() > 0)
{                    
   this.incomePaging_IdAtTheEndOfCurrentPage = incomeData.ToList()[incomeData.Count() - 1].Id;
   **this.incomePaging_IdAtTheStartOfCurrentPage = incomeData.ToList()[0].Id;**
}

I take a page data but at the bold line the incomeData object contains data of the next data page automatically. Why did the Entity Framework do like that?

+1  A: 

If you have all items in the GridView, and you use the built-in paging over all the items (ie if incomeData contains all items, and the gridview shows a section of that), then you need to look at the first visible item of IncomeGridView.

The best solution is using the PageIndex and PageSize columns:

// Bind data
this.IncomeGridView.DataSource = incomeData;

// If incomeData is not empty then Taking the value of the Id field of the first 
// and the last row in data page
if (incomeData.Any())
{         
    int pageSize = IncomeGridView.PageSize;
    int pageIndex = IncomeGridView.PageIndex;

    this.incomePaging_IdAtTheEndOfCurrentPage = incomeData
        .Skip(pageIndex * pageSize) // Skip pages before this page
        .Skip(pageSize -1)          // Skip all items except the last one
        .Take(1)                    // Take the last one
        .Id;

    this.incomePaging_IdAtTheStartOfCurrentPage = incomeData
        .Skip(pageIndex * pageSize) // Skip pages before this page
        .Take(1)                    // Take the first one
        .Id;
}
Sander Rijken
Thank Sander for help. incomeData is not contain all items, it only contains the items of a page. I'm using SQL Compact so I can not use Skip() method.
Linh