你可以嘗試這樣的..
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;
}
}
我認爲你正在尋找autocompletecombobox型..... – 2011-12-15 12:19:57