我有这个代码将数据网格视图中的一个文本框列设置为自动完成:
private void datagridWorkorderPartItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (this.datagridWorkorderPartItems.CurrentCell.ColumnIndex == 2)
{
e.Control.KeyPress += Control_KeyPress;
// e.Control.KeyDown += Control_KeyDown;
}
else if (this.datagridWorkorderPartItems.CurrentCell.ColumnIndex == 4)
{
e.Control.KeyPress += Control_PriceKeypress;
}
else
{
}
//make the part number column in the data grid view auto complete, and if the part is not in the list, need to add
TextBox textPart = e.Control as TextBox;
if (this.datagridWorkorderPartItems.CurrentCell.ColumnIndex == 3 && textPart != null)
{
textPart.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textPart.AutoCompleteSource = AutoCompleteSource.CustomSource;
Parts part = new Parts();
DataTable partdata = new DataTable();
partdata = part.LoadPartTable();
foreach (DataRow row in partdata.Rows)
{
textPart.AutoCompleteCustomSource.Add(row["PartNumber"].ToString());
}
}
else if (this.datagridWorkorderPartItems.CurrentCell.ColumnIndex != 3 && textPart != null)
{
textPart.AutoCompleteMode = AutoCompleteMode.None;
}
}
它工作得很好但现在我想处理一个与基础列表中的任何项目都不匹配的项目。它允许自由文本输入,但询问用户是否要保存新项目。
我该怎么做?
答案 0 :(得分:0)
请参阅this问题的答案。
我不建议用户询问是否将新项目保存到AutoCompleteCustomSource
。相反,它应该对用户透明。我假设您每次使用AutoCompleteCustomSource
的数据附加编辑控件时都会更新part.LoadPartTable
。如果使用用户输入的数据更新PartTable
,则下次从AutoCompleteCustomSource
设置PartTable
时,用户输入的数据将自动添加到AutoCompleteCustomSource
。