I\'ve got my DataGridView binding correctly to my datatable however I\'m trying to get autocomplete to work correctly for one of the textbox columns. The autocomplete is wor
Your if condition is just checking if the user has currently selected the third column.
Do you want to make all that column editable? or just one cell in the currently selected row? How is the edit triggered by another button off the form? In this case when the edit becomes active any cell could be selected?
You will need to index into the correct column and set it to have the autocomplete on.
try to release the prodCode.AutoCompleteCustomSource
, if it is not the correct column:
private void dataGridDetail_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridDetail.CurrentCell.ColumnIndex == 2)
{
var source = new AutoCompleteStringCollection();
String[] stringArray = Array.ConvertAll<DataRow, String>(products.Select(), delegate(DataRow row) { return (String)row["code"]; });
source.AddRange(stringArray);
TextBox prodCode = e.Control as TextBox;
if (prodCode != null)
{
prodCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
prodCode.AutoCompleteCustomSource = source;
prodCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}else
prodCode.AutoCompleteCustomSource = null;
}
Abuleen's suggestion is the best! I made just a bit improvement to it, because the line in the else statement will throw an error of type Variable does not exist in the current context
Hence from his code:
private void dataGridDetail_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if(dataGridDetail.EditingControl.GetType()==typeof(DataGridViewTextBoxEditingControl))
{
TextBox prodCode = e.Control as TextBox;
if (dataGridDetail.CurrentCell.ColumnIndex == 2)
{
var source = new AutoCompleteStringCollection();
String[] stringArray = Array.ConvertAll<DataRow, String>(products.Select(), delegate(DataRow row) { return (String)row["code"]; });
source.AddRange(stringArray);
TextBox prodCode = e.Control as TextBox;
if (prodCode != null)
{
prodCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
prodCode.AutoCompleteCustomSource = source;
prodCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
else { prodCode.AutoCompleteCustomSource = null; }
}
}