关于使用c#的gridview中的下拉列表控件

时间:2012-09-25 10:19:20

标签: c# dropdownbox

使用以下代码绑定并在网格视图中显示下拉列表。但它始终显示第一项是否数据库表具有第二个或第三个。

    private void BindData1()
    {
        DataSet7TableAdapters.sp_getall_trv_config_masterTableAdapter TA = new DataSet7TableAdapters.sp_getall_trv_config_masterTableAdapter();
        DataSet7.sp_getall_trv_config_masterDataTable DS = TA.GetData();
        if (DS.Rows.Count > 0)
        {
            GridView2.DataSource = DS;
            GridView2.DataBind();



            }
        }

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Control ctrl = e.Row.FindControl("DDL_STATUS_FT");
            if (ctrl != null)
            {
                DropDownList dd = ctrl as DropDownList;
                DataSet7TableAdapters.sp_getall_trv_masterTableAdapter TA = new DataSet7TableAdapters.sp_getall_trv_masterTableAdapter();
                DataSet7.sp_getall_trv_masterDataTable DS = TA.GetData();
                dd.DataTextField = "fld_TName";
                dd.DataValueField = "fld_id";
                dd.DataSource = DS;
                dd.DataBind();
            }

        }
}

设计代码:

  <asp:TemplateField ItemStyle-Width="100px" HeaderText="TYPE">
                    <ItemTemplate>
                        <asp:DropDownList ID="DDL_STATUS" runat="server" AutoPostBack="true" Enabled="false" >
                        </asp:DropDownList>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DDL_edit_STATUS" runat="server" AutoPostBack="true">
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="DDL_STATUS_FT" runat="server" AutoPostBack="true">
                        </asp:DropDownList>
                    </FooterTemplate>
                </asp:TemplateField>

我想将保存数据显示为选定值。但现在第一个项目始终显示在下拉列表中。请帮我这样做..

3 个答案:

答案 0 :(得分:2)

试试这个:

                    DropDownList dd = ctrl as DropDownList;
                    DataSet7TableAdapters.sp_getall_trv_masterTableAdapter TA = new DataSet7TableAdapters.sp_getall_trv_masterTableAdapter();
                    DataSet7.sp_getall_trv_masterDataTable DS = TA.GetData();
                    dd.DataTextField = "fld_TName";
                    dd.DataValueField = "fld_id";
                    dd.DataSource = DS;
                    dd.DataBind();
                    dd.SelectedValue= "";//Put your value here which needs to be selected

答案 1 :(得分:0)

由于您没有将index/value保存在数据库中的内容归还,因此每次绑定后,默认会显示第一项

答案 2 :(得分:0)

您可以在客户端的下拉列表中设置'SelectedValue',如

SelectedValue='<%# Eval("fieldname") %>'