2014-02-13 34 views
0

我現在和這個人在一起,並且正在旋轉我的輪子。 這裏是我抓住本地網絡SQL實例,並把它們放在我的combobox1中很好。將SqlDataSourceEnumerator附加到Combobox事件。 C#

SqlDataSourceEnumerator sdse = SqlDataSourceEnumerator.Instance; 
     DataTable table = sdse.GetDataSources(); 
     foreach (DataRow row in table.Rows) 
     { 
      comboBox1.Items.Add(row["ServerName"].ToString() + "\\" + row["InstanceName"].ToString()); 
     } 

當連接到按鈕單擊事件時,此功能很好用。但需要很長時間,對用戶來說不是很簡單。

我的問題是,當單擊組合框單擊時我怎麼能得到它?然後還顯示在組合框? 我希望這些操作與Windows的ODBC選擇基本相同。您單擊組合框,然後它填充。

我試過以下無濟於事。沒有錯誤,但它不會飛。

private void comboBox1_ComboDropDown(object sender, EventArgs e) 
    { 
     SqlDataSourceEnumerator sdse = SqlDataSourceEnumerator.Instance; 
     DataTable table = sdse.GetDataSources(); 
     foreach (DataRow row in table.Rows) 
     { 
      comboBox1.Items.Add(row["ServerName"].ToString() + "\\" + row["InstanceName"].ToString()); 
     } 
    } 

我也試過DropDown和MouseDown沒有效果。

回答

1

沒關係。 不得不像這樣將一個事件處理程序添加到組合框。

this.SQLComboBox.Click += new System.EventHandler(this.SQLComboBox_MouseDown); 

然後,在組合框的代碼中,我使用了這個。

private void SQLComboBox_MouseDown(object sender, EventArgs e) 
    { 
     SqlDataSourceEnumerator sdse = SqlDataSourceEnumerator.Instance; 
     DataTable table = sdse.GetDataSources(); 
     foreach (DataRow row in table.Rows) 
     { 
      SQLComboBox.Items.Add(row["ServerName"].ToString() + "\\" + row["InstanceName"].ToString()); 
     } 
    } 

工程就像一個魅力!