我有datagridview和带有单元格DataGridViewComboboxCell的列。 DisplayStyle设置为Nothing。 当我开始输入此单元格时,将其样式更改为组合框。我无法提供我的tekst,因为根据我提供的文本搜索单元格。 我想将它的样式改为comboboxlist,因为它在组合框中。我有空字段,我可以输入我的文字。在组合框中可以做到这一点吗?
由于
答案 0 :(得分: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);
}
从Form Constructor中调用这两个方法。
现在单击DataGridView并生成EditingControlShowing事件并在其中编写以下代码:
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;
}
}