0

私は、子のネストされたグリッドビューにドロップダウンリストを持たないようにしようとしています。asp.net 4 Web アプリケーションにコード ビハインドがあります。以下は私のグリッドビューのコードです。

<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 に答える