为gridview中的下拉列表设置数据源

时间:2012-09-08 04:53:55

标签: c# asp.net

我使用以下代码为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>

3 个答案:

答案 0 :(得分: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(); 

}

答案 1 :(得分: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(); 
}

答案 2 :(得分: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();
    }
}