2013-01-11 69 views
1

我有一個3列(產品,數量,價格)與頁眉和頁腳的網格視圖。我爲產品頁腳添加了一個下拉列表。現在我想將此下拉列表與數據集中的產品綁定,任何人都可以幫助我? 我以前在CS文件下面的代碼,但作爲GridView的頁腳內部的綁定下拉列表

「對象引用不設置到對象的實例即時得到附近發現控制誤差

protected void gv_page2_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     dal = new DAL(); 
     ds = new DataSet(); 
     ds=dal.DALBindFooterDDL(); 
     if (e.Row.RowType == DataControlRowType.Footer) 
     { 
      DropDownList ddl = (DropDownList)gv_page2.FooterRow.FindControl("ftrDDL"); 
      ddl.DataSource = ds.Tables[0]; 
      ddl.DataBind(); 
     } 
    } 
+2

你應該使用gridview RowDataBound事件 – Rajpurohit

回答

3

使用RowCreated(或RowDataBound事件) - 對例如

void ProductsGridView_RowCreated(Object sender, GridViewRowEventArgs e) 
{ 
    if(e.Row.RowType == DataControlRowType.FooterRow) 
    { 
     // Find the product drop-down list, you can id (or cell number) 
     var ddlProducts = e.Row.FindControl("Products") as DropDownList; 
     // var ddlProducts = e.Row.Cells[0].Controls[0]; // Finding by cell number and index 
     if (null != ddlProducts) 
     { 
      // bind to the data 
     } 
    } 
} 

免責聲明:未經測試的代碼 - 提供獲得實際soltion的想法/提示

1
protected void gv_page2_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     dal = new DAL(); 
     ds = new DataSet(); 
     ds=dal.DALBindFooterDDL(); 
     if (e.Row.RowType == DataControlRowType.Footer) 
     { 
      DropDownList ddl = e.Row.FindControl("ftrDDL") as DropDownList; 
      ddl.DataSource = ds.Tables[0]; 
      ddl.DataBind(); 
     } 
    }