2012-08-17 69 views
1

我已经尝试使用radiobuttonlist让用户选择他/她是要将当前页面的gridview还是整个gridview导出到Excel表格,但只有当前页面的工作在哪里导出整个gridview到excel工作表,它只显示标题行。这里有什么问题,是否与radiobuttonlist的selectedindex有关?如何将gridview导出为带分页的excel表格

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); 
} 
+0

我可能是错的,但是当你分配'PageSize'到某一值时,是不是真正的分页这只是说让我在网10个项目,但不能说GET只有10次毫秒从例如您的数据库并重新绑定这些数据。现在你的'this.GridView1.PageSize = 10; this.GridView1.DataBind();'不要说GridView显示所有现有的10个项目,如果你想做这个工作,你需要编写从DB分页的方法。请纠正我,如果我错了 – harry180 2012-08-17 06:26:57

回答

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); 
} 
+0

嘿,所以GetTheDataSource()是一种方法,我得到的GridView并将其填充到数据集? – user1490374 2012-08-17 07:52:00

+0

是的,你是对的 – 2012-08-17 09:03:43

0

您可以使用下面的代码...使用LINQ到特定的记录从数据源绑定到网格视图,而其他分页绑定全Datasource..then使用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); 
      }