【问题标题】:gridview binding problems with dropdownlist下拉列表的gridview绑定问题
【发布时间】:2014-04-16 09:34:04
【问题描述】:

我试图找到解决我的问题的方法,我在 gridview 中的下拉列表没有绑定,有没有办法覆盖它?

这是我得到的错误:

Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用。

谢谢

   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                DataSourceID="SqlDataSource22" EnableModelValidation="True" ForeColor="#333333"
                Width="2400px" DataKeyNames="KeyFeatureID">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" HorizontalAlign="Center"
                    VerticalAlign="Middle" />
    <Columns>
         <asp:TemplateField HeaderText="KeyFeatureID" SortExpression="KeyFeatureID" Visible="False">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("KeyFeatureID") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("KeyFeatureID") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Width="60px" />
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Category">
            <EditItemTemplate>
                <asp:DropDownList ID="DDlL" runat="server" EnableViewState="False" Style="font-size: x-small"
                                Width="200px" AppendDataBoundItems="True" DataSourceID="SqlDataSource11104" DataTextField="Category"
                                DataValueField="Category" SelectedValue='<%# Bind("Category") %>'>
                    <asp:ListItem>Category</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
            </ItemTemplate>
         </asp:TemplateField>
    </Columns>
   </asp:GridView>

【问题讨论】:

  • 不知道能不能像这样在gridview中绑定一个DropDownList。看起来好像您正在尝试调用某种 Sql 数据源来填充它。这似乎有点矫枉过正 - 您是否正在调用数据库以填充网格每一行中的 ddl?对于 Gridviews 中的下拉列表,我填充了一个包含 ddl 项列表的 DataSet - 在绑定 GridView 之前,然后在 GridView 的 RowDataBound 事件中,找到 ddl 并将其绑定到 DataSet。工作正常。如果您需要使用主 gridview 数据集中的某个值设置所选索引,您可以。

标签: c# asp.net gridview


【解决方案1】:

您必须在 RowDataBound 处理程序中绑定 DropDownList。

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Bind DropDownList here

        }
     }

【讨论】:

  • 您好,感谢您的回答,请您建议我如何将dropdownlist 绑定到.cs@Nanosoft 中
  • 如果你有SqlDatasource,DDlL.DataSourceID = YourDS.UniqueID; DDlL.DataBind();@user2910569
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-28
  • 2012-02-26
  • 2023-03-13
  • 1970-01-01
  • 2011-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多