2016-09-13 53 views
0
con = new SqlConnection(s); 
     con.Open(); 
     if (RadioButtonList1.SelectedIndex == 0) 
     { 
      cmd = new SqlCommand("select [Item] from Veg_Items", con); 
      da = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds, "[Item]"); 
      DropDownList1.DataSource = ds.Tables[0]; 
      DropDownList1.DataBind(); 
     } 
     else if (RadioButtonList1.SelectedIndex == 1) 
     { 
      cmd = new SqlCommand("select [Item] from NonVeg_Items", con); 
      da = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds, "[Item]"); 
      DropDownList1.DataSource = ds.Tables[0]; 
      DropDownList1.DataBind(); 

     } 
      con.Close(); 
    } 

我在表格中的項目,我需要一次我選擇RadioButtonList任何值顯示在Dropdownlist我的項目。 我也想象了ds.Tables[0]一行中的項目,但我無法將它們綁定到Dropdownlistasp.net下拉列表中的數據綁定在運行時

+0

您的查詢下載數據與否?問題是隻有綁定數據? –

+1

您可以嘗試添加DataTableList的DataTextField和DataValueField到DataTable中的某個列,即DropDownList1.DataTextField =「

+0

它只是獲取該表中的項目。@ mwisnicki –

回答

0
con.Open(); 
     if (RadioButtonList1.SelectedIndex == 0) 
     { 
      cmd = new SqlCommand("select [Item] from [Veg_Items]", con); 
      SqlDataReader dr = cmd.ExecuteReader(); 
      List<Object> lt = new List<Object>(); 
      if (dr.HasRows) 
      { 
       while (dr.Read()) 
       { 

        lt.Add(dr["Item"].ToString()); 
       } 
      } 
      DropDownList1.DataSource = lt; 
      DropDownList1.DataBind(); 
     } 
     else if (RadioButtonList1.SelectedIndex == 1) 
     { 
      cmd = new SqlCommand("select [Item] from [NonVeg_Items]", con); 
      SqlDataReader dr = cmd.ExecuteReader(); 
      List<Object> lt = new List<Object>(); 
      if (dr.HasRows) 
      { 
       while (dr.Read()) 
       { 

        lt.Add(dr["Item"].ToString()); 
       } 
      } 
      DropDownList1.DataSource = lt; 
      DropDownList1.DataBind(); 
    } 
    con.Close(); 
相關問題