1.前提:知道翻頁總數:ItemCount 主要思想:複製表結構,再添加當前頁應有的行和列
DataTable dt = dtResult.Clone();
DataRow dr;
for (int i = 0; i < ItemCount; i++)
{
dr = dt.NewRow();
if (i < PageCurrent * PageSize && i >= (PageCurrent - 1) * PageSize)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
try
{
dr[j] = dtResult.Rows[i - (PageCurrent - 1) * PageSize][j];
}
catch (Exception ex)
{
string s = ex.Message;
return dt;
}
}
}
dt.Rows.Add(dr);
}
2.主要思想在datatable用select篩選翻頁
protected void NavigationLink_Click(object sender, CommandEventArgs e)
{
if (CurrentPage.Value == "")
CurrentPage.Value = "0";
switch (e.CommandName)
{
case "Next":
CurrentPage.Value = "" + (Int32.Parse(CurrentPage.Value) + 1);
break;
case "Prev":
CurrentPage.Value = "" + (Int32.Parse(CurrentPage.Value) - 1);
break;
}
int iRowIndex = Convert.ToInt32(drpPagerCount.SelectedValue) * Convert.ToInt32(CurrentPage.Value) + 1;//新頁的第一行
int iNextRowIndex = Convert.ToInt32(drpPagerCount.SelectedValue) * (Convert.ToInt32(CurrentPage.Value) + 1);
DataTable dtCaseList = new DataTable();
dtCaseList = Combine2DataTable();
FormatDataTable(dtCaseList);
if (dtCaseList.Rows.Count >= iRowIndex)
{
DataTable dtResult = dtCaseList.Clone();
string expression = "row_number >=" + iRowIndex.ToString() + " and row_number <=" + iNextRowIndex.ToString();
DataRow[] d = dtCaseList.Select(expression, sort);
foreach (DataRow dr in d)
{
dtResult.Rows.Add(dr.ItemArray);
}
gvCaseList.DataSource = dtResult;
gvCaseList.DataBind();
SetChangPageAndShowCount(dtCaseList, false);
}
}