c# - 在 Gridview 中绑定(bind)没有 SqlDataSource 的 DropDownList

标签 c# asp.net gridview binding drop-down-menu

我意识到您可以使用 SqlDataSource 在 gridview 中轻松设置下拉列表,但是仅包含列表项的有限列表又如何呢?如果没有数据源,将绑定(bind)放入所选值似乎不起作用。这是我到目前为止所得到的示例。

<EditItemTemplate>
   <asp:DropDownList ID="Fund"  runat="server" SelectedValue='<%# Bind("Fund") %>' >
      <asp:ListItem  Value="">---</asp:ListItem>
      <asp:ListItem Value="Test1">Test</asp:ListItem>
      <asp:ListItem Value="Test2">Test2</asp:ListItem>
   </asp:DropDownList>
</EditItemTemplate>

这似乎是一个愚蠢的小问题,以至于我打算在数据库中创建一个静态的 10 行表。

最佳答案

最简单的解决方案是创建一个 Dictionary<TKey,TValue>并将其绑定(bind)到 DropDownList或者正如您提到的将其绑定(bind)到静态表...

示例代码:

Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("item 1", "Item 1");
list.Add("item 2", "Item 2");
list.Add("item 3", "Item 3");
list.Add("item 4", "Item 4");

ddl.DataSource = list;
ddl.DataTextField = "Value";
ddl.DataValueField = "Key";
ddl.DataBind();

关于c# - 在 Gridview 中绑定(bind)没有 SqlDataSource 的 DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11347343/

相关文章:

c# - 无法从 App.config 引用变量

c# - 回发时列表框重复(重复)

c# - 检测是否在 KeyRoutedEventArgs 事件中按下修改键

c# - Vector3.MoveTowards 没有做它的事情

c# - gRPC 与 Asp.net Core Mvc 一起运行

c# - ASP NET MVC 5 从服务器删除文件

c# - 在 asp.net 中删除行

android - 如何在应用程序中创建语音 Activity ?

c# - 如何在 c# 中删除没有 SqlDataSource 的 gridview 中的行?

c# - ASP.NET GridView EditTemplate 和查找控件