在GridView中添加下拉框来更新数据?

在GridView中添加下拉框来更新数据?

  • 前台代码

     <asp:TemplateField HeaderText="物品名称">
                                    <ItemTemplate>
                                        <asp:Label ID="Label1" runat="server" Text='<%#Eval("GoodName") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
                                    </EditItemTemplate>
                                </asp:TemplateField>
    
  • 后台代码

    • 在GridView1_RowEditing事件中进行数据绑定

      DropDownList  drop2 = GridView1.Rows[e.NewEditIndex].FindControl("DropDownList2") as DropDownList;
      //找到该控件,然后进行数据绑定
                      drop2.DataSource = BLL.Manages.Seldrop();
                      drop2.DataTextField = "GoodName";
                      drop2.DataValueField = "GoodID";
                      drop2.DataBind();
      
    • 在GridView1_RowUpdating事件中就可以获取下拉选

      DropDownList  drop=
        GridView1.Rows[e.RowIndex].Cells[3].FindControl("DropDownList2")   as  DropDownList;
      //找到这个控件
      
      string GoodID = drop.SelectedValue;
      
posted @ 2020-07-22 15:50  忘山川  阅读(504)  评论(0编辑  收藏  举报