2012-07-24 72 views
1

誰能幫助請,我想自動填充數據到我的組合框,而無需將任何按鍵,而是通過下拉控制.....從SQL Server數據庫中的數據自動填充一個combolist

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (comboBox1.AllowDrop == false) 
    { 
     SqlConnection conn = new SqlConnection("Data Source=localhost; database=KnowledgeEssentials;Trusted_Connection=yes;connection timeout=30"); 
     SqlDataAdapter adapter = new SqlDataAdapter("SELECT Problem FROM PROBLEMT", conn); 

     DataTable dt = new DataTable(); 
     DataSet ds = new DataSet(); 

     SqlDataAdapter ad = new SqlDataAdapter(); 
     ad.SelectCommand = new SqlCommand("SELECT Problem FROM PROBLEMT", conn); 
     ad.Fill(ds, "Problem"); 

     dataGridView1.DataSource = dt; 
     //Biding the data with the control 
     BindingSource bs = new BindingSource(); 
     bs.DataSource = ds; 
     bs.DataMember = "Problem"; 

     DataGridView dvg = new DataGridView(); 
     this.Controls.Add(dvg); 
     dvg.DataSource = bs; 

     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      comboBox1.Items.Add(dt.Rows[i]["Problem"]); 
     } 
    } 
    else 
    { 
    } 
} 

回答

0

你錯過了}我很難相信SelectedIndexChanged是填充組合框的最佳位置。因爲顯然你在學習,所以嘗試把你的代碼放在一個按鈕上,一旦點擊按鈕它工作的很好,你可以尋找一個更好的地方,比如表單加載。

究竟是什麼問題?它會給出錯誤嗎?

+0

沒有錯誤,該框只留空.... – 2012-07-25 10:56:04

+0

我有一個工作按鈕,它完美的作品,當我點擊它,然後它填充數據到組合框 – 2012-07-25 10:57:50

0

第一像這樣

protected void LoadCombo() 
    { 

      SqlConnection conn = new SqlConnection("Data Source=localhost;database=KnowledgeEssentials;Trusted_Connection=yes;connection timeout=30"); 
      SqlDataAdapter adapter = new SqlDataAdapter("SELECT Problem FROM PROBLEMT", conn); 
      DataSet ds = new DataSet(); 
      SqlDataAdapter ad = new SqlDataAdapter(); 
      ad.SelectCommand = new SqlCommand("SELECT Problem FROM PROBLEMT", conn); 
      ad.Fill(ds, "Problem"); 
      dataGridView1.DataSource = ds; 
      dataGridView1.DataBind(); 
     for (int X = 0; X <= ds.Tables[0].Rows.Count - 1; X++) 
     { 
      comboBox1.Items.Add(ds.Tables[0].Rows[X]["Problem"].ToString()); 
     } 

}

現在在組合框中獲取的Page_Load中調用數據和負載這種方法

protected void Page_Load() 
{ 

if(ispostback) 
{ 

} 
else 
{ 
LoadCombo(); 

} 
} 
+0

數據綁定不存在 – 2012-07-25 10:23:44

+0

ispostback在當前上下文中不存在....... – 2012-07-25 10:24:12

0

問題的答案就如何填充創建方法自動組合框:

   private void Form3_Load(object sender, EventArgs e) 
       { 
        using (SqlConnection sc = new SqlConnection()) 
        { 
        sc.ConnectionString= "database=KnowledgeEssentials;Trusted_Connection=yes;connection timeout=30"; 
        sc.Open(); 
        using (SqlDataAdapter sda = new SqlDataAdapter()) 
        { 
         DataTable data = new DataTable(); 
         sda.SelectCommand = new SqlCommand("SELECT ID, TypeProblem FROM eL_Section", sc); 
         sda.Fill(data); 

         comboBox1.ValueMember = "ID"; 
         comboBox1.DisplayMember = "ID"; 
         comboBox1.DataSource = data; 

         comboBox2.ValueMember = "TypeProblem"; 
         comboBox2.DisplayMember = "TypeProblem"; 
         comboBox2.DataSource = data; 


        } 


      } 

      using (SqlConnection cn = new SqlConnection()) 
       { 
      cn.ConnectionString = "database=KnowledgeEssentials;Trusted_Connection=yes;connection timeout=30"; 
      cn.Open(); 
       using (SqlDataAdapter da = new SqlDataAdapter()) 
        { 
        DataTable dat = new DataTable(); 
        da.SelectCommand = new SqlCommand("SELECT UserName FROM UserT", cn); 
        da.Fill(dat); 



        comboBox3.ValueMember = "UserName"; 
        comboBox3.DisplayMember = "UserName"; 
        comboBox3.DataSource = dat; 

        } 
       } 


     // TODO: This line of code loads data into the 'knowledgeEssentialsDataSet.ProblemT' table. You can move, or remove it, as needed. 
     this.problemTTableAdapter.Fill(this.knowledgeEssentialsDataSet.ProblemT); 
    }