GridView 表格中添加 DropDownList下拉列表

这个问题搞了很久,现在总结如下:

主要注意:

GridView 中的 DropDownList 控件在编译时它的 ID 已经改变,或则只编辑状态才出现,因此只能通过 下面的代码获取   DropDownList:

          //行的状态是: 编辑状态 或者 (交替行且是编辑状态)
        if (e.Row.RowState == DataControlRowState.Edit ||
            e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit)
            )
        {
            DropDownList DropDownList1 = e.Row.FindControl("nameList") as DropDownList;

            //行的状态是: 编辑状态 或者 (交替行且是编辑状态)
            ....

     }

 

 

 

 

前台主要代码:

    <asp:GridView ID="GridView1" AutoGenerateColumns="False"  runat="server" onrowediting="GridView1_RowEditing"                    
                        onrowdatabound="GridView1_RowDataBound">
              <Columns>
                  <asp:TemplateField HeaderText="姓名">                   
                        <EditItemTemplate>
                                  <asp:DropDownList ID="nameList" runat="server" >
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" DataField="user_name" Text='<%# Eval("user_name") %>'  runat="server" ></asp:Label>
                        </ItemTemplate>
                   </asp:TemplateField>
                               
                  <asp:BoundField DataField="income" HeaderText="金额" />
                  <asp:BoundField DataField="date" HeaderText="日期" />
                  <asp:CommandField HeaderText=" 命 令 " ShowDeleteButton="True"
                          ShowEditButton="True" ShowHeader="True" ShowSelectButton="True" />
              </Columns>
          </asp:GridView>

 

后台主要代码:

 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //con.Open();
        //项目
        SqlDataAdapter da = new SqlDataAdapter("select  expend_item_id, expend_item from expend_item", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "expend_item");
        //long time
            //行的状态是: 编辑状态 或者 (交替行且是编辑状态)
        if (e.Row.RowState == DataControlRowState.Edit ||
            e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit)
            )
        {
            DropDownList DropDownList1 = e.Row.FindControl("nameList") as DropDownList;

            DropDownList1.DataValueField = "expend_item_id";
            DropDownList1.DataTextField = "expend_item";
            DropDownList1.DataSource = ds.Tables["expend_item"].DefaultView;
            DropDownList1.DataBind();
          //  con.Close();
        }
    }

 

 

 

 

 

参考文章 http://www.softrc.com/MyArticle.aspx?id=40#

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值