2012-08-17 70 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); 
      }