0

我试图在我的子嵌套网格视图上有一个下拉列表,但无济于事。我的 asp.net 4 Web 应用程序有代码。下面是我的gridview的代码。

<asp:TemplateField HeaderText="Status" SortExpression="Status">
                                            <ItemTemplate><%# Eval("Status")%></ItemTemplate>
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="DropDownListStatus" runat="server">
                                                </asp:DropDownList>

                                            </EditItemTemplate>
                                            <FooterTemplate>
                                                <asp:DropDownList ID="DropDownListStatus" runat="server" >

                                                </asp:DropDownList>

我正在使用 SqlDataSource。下面是我删除了一些导致错误的代码后的代码。

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //Check if this is our Blank Row being databound, if so make the row invisible
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        if (((DataRowView)e.Row.DataItem)["CertificateNo"].ToString() == String.Empty) e.Row.Visible = false;

    }
}

我试图 FindControl 为我的下拉列表,使用查询并将其绑定到我的下拉列表,但我没有做这么简单的事情。

4

1 回答 1

0

在您后面的代码中:

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //Check if this is our Blank Row being databound, if so make the row invisible
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        if (((DataRowView)e.Row.DataItem)["CertificateNo"].ToString() == String.Empty) e.Row.Visible = false;
        //Check if is in edit mode
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            DropDownList DropDownListStatus = (DropDownList)e.Row.FindControl("DropDownListStatus");
            //Bind status data to dropdownlist
            DropDownListStatus.DataTextField = "Status";
            DropDownListStatus.DataValueField = "Status";
            DropDownListStatus.DataSource = RetrieveStatus();
            DropDownListStatus.DataBind();
            DataRowView dr = e.Row.DataItem as DataRowView;
            DropDownListStatus.SelectedValue = dr["Status"].ToString();
         }

    }
}

然后对于 RetrieveStatus:

private DataTable RetrieveStatus()
{
    //fetch the connection string from web.config
    string connString = ConfigurationManager.ConnectionStrings["sb_cpdConnectionString"].ConnectionString;
    //SQL statement to fetch entries from products
    string sql = @"Select distinct Status from cpd_certificates";
    DataTable dtStatus; = new DataTable();
    //Open SQL Connection
    using (SqlConnection conn = new SqlConnection(connString))
    {
        conn.Open();
        //Initialize command object
        using (SqlCommand cmd = new SqlCommand(sql, conn))
        {
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            //Fill the result set
            adapter.Fill(dtStatus;);
        }
    }
    return dtStatus;
}

经过测试和工作最终。

于 2013-03-15T10:00:22.333 回答