GridView DropDownList 数据 绑定 RowEditing

本文介绍了如何在ASP.NET的GridView中使用DropDownList进行数据编辑绑定。首先,通过设置GridView的编辑列模板,将DropDownList绑定到数据源。在数据绑定事件中,设置DropDownList的SelectedValue为当前行要更新的值。然后,通过SqlDataSource完成数据的更新操作,实现了在GridView中使用DropDownList进行编辑的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、protected void YycfBindDDL_RowEditing(object sender, GridViewEditEventArgs e)
  {
    YycfBindDDL.EditIndex = e.NewEditIndex;
    YycfBindDDL.DataSource = ds;
    YycfBindDDL.DataBind();

    DropDownList ddl = (DropDownList)YycfBindDDL.Rows[e.NewEditIndex].FindControl("ddl");
    sql = "select testname from yamzef group by testname";
    ds = db.getDataset(sql, "ddl");
    ddl.DataSource = ds.Tables["ddl"];
    ddl.DataValueField = "testname";
    ddl.DataMember = "testname";
    ddl.SelectedValue = YycfBindDDL.DataKeys[e.NewEditIndex].Value.ToString();
    ddl.DataBind();
  }

 
2、首先,要把编辑列设模板列,然后dropdownlist绑定数据源
然后在databinding里把selectedvalue的值绑定到要更新的列名上

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=False  DataKeyNames="id"
                        DataSourceID="SqlDataSource1">
                        <Columns>
                            <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
                            <asp:TemplateField HeaderText="yycf" SortExpression="yycf">
                                <EditItemTemplate>
                                    &nbsp;<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
                                        DataTextField="power_id" DataValueField="power_id" SelectedValue='<%# Bind("yycf") %>'>
                                    </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:cmssConnectionString %>"
                                        SelectCommand="SELECT [power_id] FROM [power]"></asp:SqlDataSource>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("yycf") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ShowEditButton="True" />
                        </Columns>
                       
                    </asp:GridView>
                </td>
            </tr>
            <tr>
                <td style="width: 73px">
                </td>
                <td style="width: 100px">
                    &nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server" UpdateCommand ="update admin set yycf=@yycf where id=@original_id"  OldValuesParameterFormatString ="original_id" ConnectionString="<%$ ConnectionStrings:cmssConnectionString %>"
                        SelectCommand="SELECT [yycf], [id] FROM [admin]"></asp:SqlDataSource>
                </td>

OK,就这样搞定了!~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值