Textbox Autocomplete in a DataGridView Winform

后端 未结 3 1337
轻奢々
轻奢々 2020-12-18 16:50

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

相关标签:
3条回答
  • 2020-12-18 17:07

    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.

    0 讨论(0)
  • 2020-12-18 17:15

    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;
    }
    
    0 讨论(0)
  • 2020-12-18 17:15

    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; }
        }
    }
    
    0 讨论(0)
提交回复
热议问题