2013-08-02 77 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

嗯..得到它..会有一个尝试。现在它似乎在工作。请检查我的新代码和我的新错误。 –