2013-03-15 26 views
0

我试图无法在我的子项嵌套gridview上使用dropdownlist。我有我的asp.net 4 web应用程序后面的代码。 以下是我的gridview的代码。使用我的编辑和页脚模板中的数据库中的数据在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已为我的下拉列表,使用查询,并将其绑定到我的下拉列表,但我没有做这样简单的事情。

回答

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; 
} 

测试和工作决赛。

相关问题