2013-08-02 124 views
1

我必須顯示並提取100k +記錄表。將分頁添加到我的ASP.NET GridView

我正在使用GridView,但它沒有顯示數據,因爲發生了memoryException。

所以我想添加分頁系統到我的GridView。我嘗試過各種教程,但是當使用gridview加載頁面時,所有教程都涵蓋了。但在我的情況下,GridView會在請求按下按鈕時加載。

如何綁定我的分頁代碼,所以每頁顯示10-20條記錄?

這裏是我的代碼隱藏:

protected void ExportToExcel(object sender, EventArgs e) 
{ 
    Response.Clear(); 
    Response.Buffer = true; 
    Response.AddHeader("content-disposition", "attachment;filename=Pfilename.xls"); 
    Response.Charset = ""; 
    Response.ContentType = "application/vnd.ms-excel"; 

    using (StringWriter sw = new StringWriter()) 
    { 
     HtmlTextWriter hw = new HtmlTextWriter(sw); 

     //To Export all pages 
     GridView1.AllowPaging = false; 


     GridView1.HeaderRow.BackColor = Color.White; 
     foreach (TableCell cell in GridView1.HeaderRow.Cells) 
     { 
      cell.BackColor = GridView1.HeaderStyle.BackColor; 
     } 
     foreach (GridViewRow row in GridView1.Rows) 
     { 
      row.BackColor = Color.White; 
      foreach (TableCell cell in row.Cells) 
      { 
       if (row.RowIndex % 2 == 0) 
       { 
        cell.BackColor = GridView1.AlternatingRowStyle.BackColor; 
       } 
       else 
       { 
        cell.BackColor = GridView1.RowStyle.BackColor; 
       } 
       cell.CssClass = "textmode"; 
       List<Control> controls = new List<Control>(); 

       //Add controls to be removed to Generic List 
       foreach (Control control in cell.Controls) 
       { 
        controls.Add(control); 
       } 

       //Loop through the controls to be removed and replace then with Literal 
       foreach (Control control in controls) 
       { 
        switch (control.GetType().Name) 
        { 
         case "HyperLink": 
          cell.Controls.Add(new Literal { Text = (control as HyperLink).Text }); 
          break; 
         case "TextBox": 
          cell.Controls.Add(new Literal { Text = (control as TextBox).Text }); 
          break; 
         case "LinkButton": 
          cell.Controls.Add(new Literal { Text = (control as LinkButton).Text }); 
          break; 
         case "CheckBox": 
          cell.Controls.Add(new Literal { Text = (control as CheckBox).Text }); 
          break; 
         case "RadioButton": 
          cell.Controls.Add(new Literal { Text = (control as RadioButton).Text }); 
          break; 
        } 
        cell.Controls.Remove(control); 
       } 
      } 
     } 

     GridView1.RenderControl(hw); 

     //style to format numbers to string 
     string style = @"<style> .textmode { mso-number-format:\@; } </style>"; 
     Response.Write(style); 
     Response.Output.Write(sw.ToString()); 
     Response.Flush(); 
     Response.End(); 
    } 
} 

protected void ViewPP_Click(object sender, EventArgs e) 
{ 
    string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 
    using (SqlConnection con = new SqlConnection(strConnString)) 
    { 
     using (SqlCommand cmd = new SqlCommand("SELECT * FROM Table")) 
     { 
      using (SqlDataAdapter sda = new SqlDataAdapter()) 
      { 
       cmd.Connection = con; 
       sda.SelectCommand = cmd; 

        using (DataTable dt = new DataTable()) 
        { 
         sda.Fill(dt); 
         GridView1.DataSource = dt; 
         GridView1.DataBind(); 
        } 
       } 
     } 
    } 
} 

更新我用這個代碼是最後一部分:

protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    GridView1.PageIndex = e.NewPageIndex; 
    GridView1.DataBind(); 
} 

的問題是,當我導出到Excel文件。它會在excel文件中創建分頁,並且不能像它應該那樣工作。它顯示在Excel表中分頁,並且點擊不起作用。

+0

你嘗試谷歌甚至有點,說實話? – abatishchev

+0

谷歌搜索後,我得到了綁定的答覆..但不像@ nunespascal回答完美! –

回答

1

尋呼是一個非常基本的任務

這裏是一個教程,指導您:Paging and Sorting the GridView's Data

<asp:GridView ID="GridView1" Runat="server" 
    AutoGenerateColumns="False" 
    AllowPaging="True" > 

的主要問題是不過您加載所有的數據到一個DataTable。這將加載內存中的所有數據。您應該使用SqlDataSource代替。上面的教程還向您展示瞭如何使用SqlDataSource。

編輯

SqlDataSource的設置按鈕點擊:

protected void Button_Click(object sender, EventArgs e) 
{ 
    GridView1.DataSource = SqlDataSource1; 
    GridView1.DataBind(); 
} 
+0

我得到了分頁解決方案,但加載大數據我不明白你的意思是由sqlDataSource的用法。因爲SqlDataSource的用法會在頁面加載時顯示內容。而不是用戶點擊按鈕。 –

+0

它會在綁定SqlDataSoruce時顯示數據。你可以點擊按鈕來做到這一點。 – nunespascal

+0

嗯..得到它..會有一個嘗試。現在它似乎在工作。請檢查我的新代碼和我的新錯誤。 –