使用我的编辑和页脚模板中的数据库中的数据填充gridview中的下拉列表

时间:2013-03-15 08:40:19

标签: c# asp.net

我试图在我的子嵌套gridview上有一个下拉列表无效。我的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,使用查询并将其绑定到我的下拉列表,但我没有做这么简单的事情。

1 个答案:

答案 0 :(得分: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;
}

经过测试和工作决赛。