2011-11-28 24 views
0

我想在ASP.Net的GridView中進行自定義分頁,我的數據源是一個對象數據。 我檢查了網絡找到任何代碼,發現一個不適合我的工作。ASP.Net中的自定義分頁

protected void invoiceReportsgridView_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
     if (sender != null) 
     { 
      displayInvListgridView.PageIndex = e.NewPageIndex; 
      displayInvListgridView.EditIndex = -1; 
      displayInvListgridView.SelectedIndex = -1; 
     } 
    } 

    protected void GridView_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.Pager) 
     { 
      CustomizePageBar(e); 
     } 
    } 

    void ProductsGridView_RowCommand(Object sender, GridViewCommandEventArgs e) 
    { 
     // If multiple buttons are used in a GridView control, use the 
     // CommandName property to determine which button was clicked. 
     if (e.CommandName == "Add") 
     { 
      // Convert the row index stored in the CommandArgument 
      // property to an Integer. 
      int index = Convert.ToInt32(e.CommandArgument); 

      // Retrieve the row that contains the button clicked 
      // by the user from the Rows collection. 
      GridViewRow row = displayInvListgridView.Rows[index]; 

      // Create a new ListItem object for the product in the row.  
      ListItem item = new ListItem(); 
      item.Text = Server.HtmlDecode(row.Cells[1].Text); 

      // If the product is not already in the ListBox, add the ListItem 
      // object to the Items collection of the ListBox control. 
      //if (!displayInvListgridView.Items.Contains(item)) 
      //{ 
      // displayInvListgridView.Items.Add(item); 
      //} 
     } 
    } 

    private void CustomizePageBar(GridViewRowEventArgs e) 
    { 

     //Table tblPager = new Table(); 
     tblPager.BorderWidth = 0; 
     tblPager.CellPadding = 0; 
     tblPager.CellSpacing = 0; 
     tblPager.Width = Unit.Percentage(100); 
     tblPager.Height = Unit.Pixel(20); 

     //add a row for our pager contents 
     tblPager.Rows.Add(new TableRow()); 

     //Spacer Cell 
     TableCell tcelSpace = new TableCell(); 
     tcelSpace.Width = Unit.Pixel(360); 


     //Page x of y Cell 
     TableCell tcelXofY = new TableCell(); 
     tcelXofY.Width = Unit.Pixel(100); 

     Label litXofY = new Label(); 
     litXofY.Text = "Page " + (displayInvListgridView.PageIndex + 1) + " of " + displayInvListgridView.PageCount; 
     litXofY.Font.Bold = true; 
     tcelXofY.Controls.Add(litXofY); 


     //lable GoTo 
     Label lblGoto = new Label(); 
     lblGoto.Text = "GoTo: "; 
     lblGoto.ID = "lblGoTo"; 
     lblGoto.Font.Bold = true; 
     lblGoto.Font.Size = displayInvListgridView.PagerStyle.Font.Size; 

     TableCell tcelGoto = new TableCell(); 
     tcelGoto.Width = Unit.Pixel(25); 
     tcelGoto.Controls.Add(lblGoto); 


     //Pick drop downlist 
     TableCell tcelPickPage = new TableCell(); 
     tcelPickPage.Width = Unit.Pixel(25); 
     //The dropdown list box 
     DropDownList ddlPickPage = new DropDownList(); 
     ddlPickPage.ID = "ddlPick"; 
     ddlPickPage.AutoPostBack = true; 
     ddlPickPage.EnableViewState = true; 
     ddlPickPage.Font.Size = displayInvListgridView.PagerStyle.Font.Size; 

     for (int index = 1; index <= displayInvListgridView.PageCount; index++) 
     { 
      ddlPickPage.Items.Add(index.ToString()); 
     } 
     ddlPickPage.SelectedIndex = displayInvListgridView.PageIndex; 

     //handle event for picklist 
     ddlPickPage.SelectedIndexChanged += new EventHandler(OnPagePicked); 
     tcelPickPage.Controls.Add(ddlPickPage); 

     //The existing Nav controls 
     TableCell tcelNav = new TableCell(); 
     tcelNav.Width = Unit.Pixel(150); 

     //for move all existing controls 
     foreach (Control ctrl in e.Row.Cells[0].Controls) 
     { 
      tcelNav.Controls.Add(ctrl); 
     } 

     // add all cells to new pager 

     tblPager.Rows[0].Cells.Add(tcelSpace); 
     tblPager.Rows[0].Cells.Add(tcelXofY); 
     tblPager.Rows[0].Cells.Add(tcelGoto); 
     tblPager.Rows[0].Cells.Add(tcelPickPage); 
     tblPager.Rows[0].Cells.Add(tcelNav); 


     //replace grids pager with new 
     e.Row.Cells[0].Controls.Add(tblPager); 

    } 


    protected void OnPagePicked(object sender, EventArgs e) 
    { 
     DropDownList ddlPick = (DropDownList)sender; 
     displayInvListgridView.PageIndex = Convert.ToInt32(ddlPick.SelectedItem.Value) - 1; 
     //Raise page index changed so user can rebind data 

     GridViewPageEventArgs gvArgs = new GridViewPageEventArgs(Convert.ToInt32(ddlPick.SelectedItem.Value) - 1); 
     //OnPageIndexChanging(gvArgs); 
     GridViewPageEventArgs ex = new GridViewPageEventArgs(Convert.ToInt32(ddlPick.SelectedItem.Value) - 1); 
     invoiceReportsgridView_PageIndexChanging(sender, gvArgs); 
    } 
+0

請檢查您的問題中發佈的代碼。你有超過幾行註釋掉 - 請刪除不必要的行,並確保您發佈的示例實際編譯。如果將來有人發現這個問題,我們希望他們能夠使用這裏的知識。 – jwiscarson

回答

1

手動分頁的GridView背後的想法是,你需要重新綁定在每一頁上改變GridView的,也需要改變頁面的索引。這裏有一個簡單的例子:

//This is the method to bind the gridview to the data. 
private void LoadGridview() 
{ 
    List<MyObject> MyObjectList = MyObject.FillMyList(); 
    if (MyObjectList != null && MyObjectList.Count > 0) 
    { 
     this.GridView1.DataSource = MyObjectList; 
     this.GridView1.DataBind(); 
    } 
} 

//When the index changes 
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    //Changes the page 
    this.GridView1.PageIndex = e.NewPageIndex; 
    LoadGridview(); 
} 
protected void Page_Load(object sender, EventArgs e) 
{ 
    //When the page loads 
    LoadGridview(); 
} 

祝你好運!

+0

感謝一噸漢尼特。我在這裏坐了幾個小時。再次感謝。 – SunVigna

+0

沒問題,祝你好運! –