2015-05-06 84 views
7

我有一個ComboBoxc# Windows窗體應用程序,我已經設置AutoCompleteModeSuggestAppend,並且文本自動附加到輸入(圖1)。如何建議在DataGridView中添加ComboBox?

但是,如果我設置AutoCompleteModeSuggestAppendDataGridView ComboBox它不附加文本(圖2)。

如何在datagridview組合框中啓用SuggestAppend

圖1:

AutoComplete ComboBox

圖2:

AutoComplete DataGridViewComboBoxCell

+0

看看[這篇文章](http://stackoverflow.com/a/5978017/1014320)。這對我很有用。 – vaheeds

回答

4

你以爲你會做它就像正常ComboBox

this.comboBox1.AutoCompleteCustomSource = new AutoCompleteStringCollection(); 
this.comboBox1.AutoCompleteCustomSource.AddRange(new string[] { "Good night", "Good evening", "Good", "All Good", "I'm Good" }); 
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend; 
this.comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; 

有了預期的結果:

AutoComplete ComboBox

事實證明,你可以!但是,選定的選項在您離開單元格後將不會保留。我發現你必須改變如何添加下拉選項,你如何在源它們:

public Form1() 
{ 
    InitializeComponent(); 
    DataGridViewComboBoxColumn cc = new DataGridViewComboBoxColumn(); 
    cc.Name = "Combo"; 
    cc.Items.AddRange(new string[] { "Good night", "Good evening", "Good", "All Good", "I'm Good" }); 
    this.dataGridView1.Columns.Add(cc); 
} 

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
{ 
    ComboBox box = e.Control as ComboBox; 
    if (box != null) 
    { 
    box.DropDownStyle = ComboBoxStyle.DropDown; 
    box.AutoCompleteSource = AutoCompleteSource.ListItems; 
    box.AutoCompleteMode = AutoCompleteMode.SuggestAppend; 
    } 
} 

這將提供您所需的結果:

AutoComplete DataGridViewComboBoxCell

+0

謝謝@OhBeWise你的時間,其實我寫了相同的代碼,但建議我只寫了建議:) – tharif

0

這裏是一個快速如何在Windows應用程序的DataGridView中使用AutoComplete組合框的示例。

創建一個Windows應用程序並從工具箱添加DataGridView進行設計。 現在創建兩個DataGridViewComboBoxColumns並將其添加到DataGridView:

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

enter image description here

這工作了這是目前在DataGridView所有組合框。

this post得到。