Reputation: 119
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 working but I'm trying to restrict it to only one column on the datagridview. At the moment it either does it for every column, or nothing at all. Any ideas? Code below.
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;
}
}
}
Upvotes: 2
Views: 18718
Reputation: 2015
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; }
}
}
Upvotes: 1
Reputation: 2801
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.
Upvotes: 2
Reputation: 543
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;
}
Upvotes: 7