0

我正在使用以下代码为我的 gridview 中的下拉菜单设置数据源,但它不起作用。

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

    }

list 是一个字符串列表,它将 find 绑定到我在 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>
4

3 回答 3

0

gridview 中的下拉菜单folders不是folderlist. 在 e.Row.FindControl() 方法中提供文件夹。调试代码可以帮助你发生什么。还要确保列表中有数据。

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

}
于 2012-09-08T05:07:50.747 回答
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(); 
}
于 2012-09-08T05:42:43.187 回答
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();
    }
}
于 2017-09-09T06:45:42.120 回答