0

我正在使用以下代码绑定并在网格视图中显示下拉列表。但它总是显示第一项,无论数据库表有第二项还是第三项。

    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>

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

4

3 回答 3

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
于 2012-09-25T10:49:06.110 回答
0

You can set the 'SelectedValue' on your dropdownlist on clientside like

SelectedValue='<%# Eval("fieldname") %>'
于 2012-09-25T10:39:53.833 回答
0

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

于 2012-09-25T10:28:05.197 回答