1

我尝试使用单选按钮列表让用户选择是否要将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);
}
4

2 回答 2

1

只是一个想法

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);
}
于 2012-08-17T07:32:37.103 回答
0

您可以使用以下代码...使用 Linq 将特定记录从 Datasource 绑定到 Grid 视图,同时分页绑定整个 Datasource ..然后使用 Gridview 到您的 Export 方法.. 这应该可以工作...

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); 
           } 
于 2012-08-17T08:09:16.640 回答