Textbox Autocomplete in a DataGridView Winform

17,798

Solution 1

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.

Solution 2

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;
}

Solution 3

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; }
    }
}
Share:
17,798
petedotg
Author by

petedotg

Updated on June 18, 2022

Comments

  • petedotg
    petedotg almost 2 years

    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;
    
                }
            }
        }
    
  • petedotg
    petedotg over 12 years
    Yes, make the whole third column editable with autocomplete. As far as I understand the above code... the editing control showing is fired whenever a cell is being edited in the datagridview. I then check to make sure that only the third column receives the auto complete.
  • kmcc049
    kmcc049 over 12 years
    no you check to see that the user has currently clicked on a cell in the datagrid in the third column.
  • petedotg
    petedotg over 12 years
    Ok worked it out. Thanks for pointing me in the right direction. I'll post my solution below later on today.
  • andrew
    andrew over 12 years
    This is exactly what I want to do today.. it would be great if your solution was posted but i'll dry and debug the above.
  • Hisham
    Hisham over 7 years
    prodCode out of scope please fix it