2011-12-15 42 views
1

我有datagridview和單元格DataGridViewComboboxCell列。 DisplayStyle被設置爲Nothing。 當我開始在這個單元格中輸入時,它的樣式會改變爲組合框。而且我無法提供我的技術,因爲根據我提供的文本來搜索項目。 我想將它的樣式更改爲comboboxlist,因爲它在組合框中。我有空的地方可以輸入我的文字。可以在組合框中做到這一點?如何使用DisplayStyle ComboBoxList創建組合框單元格?

感謝

+1

我認爲你正在尋找autocompletecombobox型..... – 2011-12-15 12:19:57

回答

2

你可以嘗試這樣的..

public void ComboList1() 
    { 
     DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn(); 
     combo1.HeaderText = "Country"; 
     combo1.Items.Add("Antarctica"); 
     combo1.Items.Add("Belgium"); 
     combo1.Items.Add("Canada"); 
     combo1.Items.Add("Finland"); 
     combo1.Items.Add("Albania"); 
     combo1.Items.Add("India"); 
     combo1.Items.Add("Barbados"); 
     dataGridView1.Columns.Add(combo1); 
    } 
    public void ComboList2() 
    { 
     DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn(); 
     combo2.HeaderText = "Types of Jobs"; 
     combo2.Items.Add("Accounting"); 
     combo2.Items.Add("HR"); 
     combo2.Items.Add("Finance"); 
     combo2.Items.Add("Transportation"); 
     combo2.Items.Add("Testing"); 
     dataGridView1.Columns.Add(combo2); 
    } 

呼叫無論從表格構造這些方法。

單擊DataGridView中併產生EditingControlShowing事件,並寫在它的folllowing代碼:

 if (e.Control is DataGridViewComboBoxEditingControl) 
     { 
      ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown; 
      ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems; 
      ((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest; 
     } 

或嘗試這樣

private string[] list; 
private DataSet ds; 

public frmMain() 
{ 
    InitializeComponent(); 
    ds = this.GetDataSet("SELECT deptName from empDept", "empDept"); 
    list = new string[ds.Tables[0].Rows.Count]; 
    comboBox1.AutoCompleteCustomSource.AddRange(list); 
    comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend; 
    comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems; 
} 


private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
{ 
    if (e.Control is ComboBox) 
    { 
    ComboBox cb = e.Control as ComboBox; 
    cb.DropDownStyle = ComboBoxStyle.DropDown; 

    } 
} 
相關問題