C# 如何通过分页将gridview导出到excel工作表

C# 如何通过分页将gridview导出到excel工作表,c#,asp.net,export-to-excel,C#,Asp.net,Export To Excel,我尝试使用radiobuttonlist让用户选择是否要将gridview的当前页面或整个gridview导出到excel工作表,但仅限于当前页面,而对于将整个gridview导出到excel工作表,它仅显示标题行。这里的问题是什么,是否与radiobuttonlist的selectedindex有关 protected void btnExport_Click(object sender, EventArgs e) { if (this.RadioButtonList1.Selecte

我尝试使用radiobuttonlist让用户选择是否要将gridview的当前页面或整个gridview导出到excel工作表,但仅限于当前页面,而对于将整个gridview导出到excel工作表,它仅显示标题行。这里的问题是什么,是否与radiobuttonlist的selectedindex有关

protected void btnExport_Click(object sender, EventArgs e)
{
    if (this.RadioButtonList1.SelectedIndex == 1)
    {
        //  the user wants all rows in the current page, set pagesize and rebind
        this.GridView1.PageSize = 10;
        this.GridView1.DataBind();
    }
    else if (this.RadioButtonList1.SelectedIndex == 2)
    {
        //  the user wants all rows exported, have to turn off paging and rebind
        this.GridView1.AllowPaging = false;
        this.GridView1.DataBind();
    }
    GridViewExportUtil.Export("ContactsInformation.xls", this.GridView1);
}
只是一个想法

protected void btnExport_Click(object sender, EventArgs e)
{
    var collection = GetTheDataSource(); // Get the source.

    if (this.RadioButtonList1.SelectedIndex == 1)
    {
        //  take first 10 items from the collection     
        collection = collection.Take(10); 
    }
    //set the grid source followed by binding it
    this.GridView1.DataSource = collection;
    this.GridView1.DataBind();

    //Export the grid record to excel
    GridViewExportUtil.Export("ContactsInformation.xls", this.GridView1);
}
只是一个想法

protected void btnExport_Click(object sender, EventArgs e)
{
    var collection = GetTheDataSource(); // Get the source.

    if (this.RadioButtonList1.SelectedIndex == 1)
    {
        //  take first 10 items from the collection     
        collection = collection.Take(10); 
    }
    //set the grid source followed by binding it
    this.GridView1.DataSource = collection;
    this.GridView1.DataBind();

    //Export the grid record to excel
    GridViewExportUtil.Export("ContactsInformation.xls", this.GridView1);
}

您可以使用以下代码…使用Linq将特定记录从数据源绑定到网格视图,同时分页,否则将绑定整个数据源..然后使用网格视图导出方法。。 这应该有用

protected void btnExport_Click(object sender, EventArgs e) 
        {    
              var datasource;
              if (this.RadioButtonList1.SelectedIndex == 1)     
               {         
                  int pageSize = 10; 
                  datasource= GridViewDatasourceCollection.Skip(pageSize * pageNumber).Take(pageSize);    
                  this.GridView1.DataSource= datasource;    
                  this.GridView1.DataBind();     
               }     
               else if (this.RadioButtonList1.SelectedIndex == 2)     
               {         
                  //  the user wants all rows exported, have to turn off paging and rebind                
                  this.GridView1.AllowPaging = datasource;         
                  this.GridView1.DataBind();     
               }     
               GridViewExportUtil.Export("ContactsInformation.xls", this.GridView1); 
           } 

您可以使用以下代码…使用Linq将特定记录从数据源绑定到网格视图,同时分页,否则将绑定整个数据源..然后使用网格视图导出方法。。 这应该有用

protected void btnExport_Click(object sender, EventArgs e) 
        {    
              var datasource;
              if (this.RadioButtonList1.SelectedIndex == 1)     
               {         
                  int pageSize = 10; 
                  datasource= GridViewDatasourceCollection.Skip(pageSize * pageNumber).Take(pageSize);    
                  this.GridView1.DataSource= datasource;    
                  this.GridView1.DataBind();     
               }     
               else if (this.RadioButtonList1.SelectedIndex == 2)     
               {         
                  //  the user wants all rows exported, have to turn off paging and rebind                
                  this.GridView1.AllowPaging = datasource;         
                  this.GridView1.DataBind();     
               }     
               GridViewExportUtil.Export("ContactsInformation.xls", this.GridView1); 
           } 

我可能错了,但当您将
PageSize
分配给某个不是真正分页的值时,它只是说在网格中显示10项,而不是说仅从数据库中获取10项,然后重新绑定此数据。现在您的
this.GridView1.PageSize=10;this.GridView1.DataBind()
DoNothing只说GridView显示所有现有项目中的10个项目,若要实现这一点,需要编写从DB分页的方法。如果我错了,请纠正我,我可能错了,但当您将
PageSize
分配给某个不是真正分页的值时,它只是说在网格中显示10项,而不是说仅从数据库中获取10项,然后重新绑定此数据。现在您的
this.GridView1.PageSize=10;this.GridView1.DataBind()
DoNothing只说GridView显示所有现有项目中的10个项目,若要实现这一点,需要编写从DB分页的方法。如果我错了,请纠正我嘿,那么gethedatasource()是一个获取gridview并将其填充到数据集的方法吗?嘿,那么gethedatasource()是一个获取gridview并将其填充到数据集的方法吗?