asp.net – 如何绑定gridview中的下拉列表?

我有一个gridview,其中每一行都包含一个下拉列表.我想动态绑定每个下拉列表.有人能告诉我我该怎么做.提前致谢

解决方法

如果您使用的是模板列,则可以使用数据绑定表达式从标记绑定下拉列表.例如,
<asp:TemplateField HeaderText="XYZ">
  <ItemTemplate>
    <asp:DropDownList runat="server" ID="MyDD" DataSourceId="MyDataSource" />
  </ItemTemplate> 
</asp:TemplateField>

以上假设您的下拉数据在行间不变.如果它正在改变那么你可以使用数据绑定表达式,如

<asp:DropDownList runat="server" DataSource='<%# GetDropDownData(Container) %>' DataTextField="Text" DataValueField="Value"  />

GetDropDownData将是代码隐藏中的受保护方法,它将返回给定行的数据(数据表,列表,数组).

您可以在代码隐藏中使用GridView.RowDataBound事件(或RowCreated事件)来填充下拉列表.例如,

protected void GridView_RowDataBound(Object sender,GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DaTarow)
    {
      // Find the drop-down (say in 3rd column)
      var dd = e.Row.Cells[2].Controls[0] as DropDownList;
      if (null != dd) {
         // bind it
      }

      /*
      // In case of template fields,use FindControl
      dd = e.Row.Cells[2].FindControl("MyDD") as DropDownList;
      */
    }

  }

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....