2012-09-08 94 views
0

我正在使用下面的代碼來爲我的gridview中的下拉菜單設置一個數據源,但它不工作。設置gridview下拉列表的datasource

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     DropDownList drop = (DropDownList)e.Row.FindControl("folderslist"); 
     drop.DataSource = list; 
     drop.DataBind(); 

    } 

列表是一個字符串列表,結合找到一個下拉我有我的GridView之外。但是,上面的代碼不會填充gridview中的下拉菜單。

<asp:GridView ID="GridView1" HorizontalAlign="Center" 
           AutoGenerateColumns="false" CellSpacing="5" CellPadding="5" CssClass="GridView" Font-Size="Small" 
           runat="server" OnRowDataBound = "GridView1_RowDataBound" > 

         <Columns> 


         <asp:BoundField HeaderText="From" DataField="From" /> 

         <asp:BoundField HeaderText="Subject" DataField="Subject" /> 

         <asp:BoundField HeaderText="Received" DataField="Received" /> 





         <asp:TemplateField HeaderText="Attachments" ItemStyle-HorizontalAlign="Center"> 
            <ItemTemplate> 
             <asp:ImageButton runat="server" ID="attach" CommandName="viewattachments" Text="More" 
              ImageUrl="~/images/notes.png" visible='<%# System.Convert.ToBoolean((DataBinder.Eval(Container.DataItem, "Attachments").ToString() == "") ? false : true) %>' /> 


            </ItemTemplate> 
            <ItemStyle HorizontalAlign="Center"></ItemStyle> 
           </asp:TemplateField> 


           <asp:TemplateField HeaderText="Move To" ItemStyle-HorizontalAlign="Center"> 
            <ItemTemplate> 

             <asp:DropDownList ID="folderslist" runat="server"></asp:DropDownList> 

            </ItemTemplate> 
            <ItemStyle HorizontalAlign="Center"></ItemStyle> 
           </asp:TemplateField> 

</Columns> 
          </asp:GridView> 
+0

你能告訴我們HTML一側GridView和下拉列表是什麼? – Adil

回答

0

下拉在GridView控件是foldersfolderlist。在e.Row.FindControl()方法中給文件夾。調試代碼可以幫助你發生什麼事情。還要確保列表中有數據。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    DropDownList drop = (DropDownList)e.Row.FindControl("folders"); 
    drop.DataSource = list; 
    drop.DataBind(); 

} 
+0

只是改變了我正在測試的東西。他們被正確命名。 – user541597

+0

你調試過代碼嗎? – Adil

+0

是的。你調用的對象是空的。 – user541597

0

asp:DropDownList綁定到數據源項目的屬性,並且String類型沒有返回其值的屬性。嘗試每個字符串包裝成一個輔助對象:

class StringHolder 
{ 
     public StringHolder(string displayText) { DisplayText = displayText; } 
     public string DisplayText { get; set;} 
} 


IList<StringHolder> WrapStrings(IList<string> strings) 
{ 
    return strings.Select(it => new StringHolder(it)).ToList()); 

} 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    DropDownList drop = (DropDownList)e.Row.FindControl("folderslist"); 
    drop.DataTextField = "DisplayText"; 
    drop.DataSource = WrapStrings(list); 
    drop.DataBind(); 
} 
+1

我在drop.DataTextField =「DisplayText」中得到一個錯誤「Object reference not set to a instance of a object。」。線。 – user541597

+0

@ user541597查看我最近的修改 – alexm

+0

我將如何添加到列表中?我在使用列表,還是已經定義了它? – user541597

0
private void grdCoboFill() 
{ 
    DataTable dt = new DataTable(); 
    dt.Columns.Add("ColorID", typeof(int)); 
    dt.Columns.Add("ColorName", typeof(String)); 

    dt.Rows.Add(new Object[] { 1, "RED" }); 
    dt.Rows.Add(new Object[] { 2, "GREEN" }); 
    dt.Rows.Add(new Object[] { 3, "BLUE" }); 

    foreach (GridViewRow row in this.GridView1.Rows) 
    { 
     ((DropDownList)row.FindControl("DropDownList1")).DataSource = dt; 
     ((DropDownList)row.FindControl("DropDownList1")).DataValueField = "ColorID"; 
     ((DropDownList)row.FindControl("DropDownList1")).DataTextField = "ColorName"; 
     ((DropDownList)row.FindControl("DropDownList1")).DataBind(); 
    } 
}