how to get the editing control of datagridview/datagridviewcell?

14,668

I don't think you can access the editing control of a cell unless that cell is in edit mode. I think the cell doesn't have an edit control until it enters edit mode. This is probably why there is no EditingControl property on the cell, but there is one the DataGridView.

You can get the type of editing control of a cell using the EditType property of the cell, and you can get the current edit control with the DataGridView.EditingControl property.

if (dgv.CurrentCell.EditType == typeof(DataGridViewTextBoxEditingControl))     
{         
    ((TextBox)dgv.EditingControl).AutoCompleteSource =
        AutoCompleteSource.CustomSource; 
}
Share:
14,668
nawfal
Author by

nawfal

You should accept the answer if it helped you by selecting the green tick mark on the top left of the answer. That's the encouraged practice at SO. Not only it helps future visitors, it encourages users to answer your questions. You should post what you have tried and tell us where you're stuck. That gives us more detail, and we can quickly give an answer by copy-pasting the code with minor modifications. Questions like I-need-this,-give-me-code doesnt work in SO. In the current format the question will be closed. You may read http://stackoverflow.com/faq additionally before posting. Do a good search on SO to ensure you're not asking a duplicated question, or else your question will be closed. Quick tag search

Updated on July 02, 2022

Comments

  • nawfal
    nawfal almost 2 years

    I have a datagridviewcell with textbox as the control hosted by it. Now how do I get the type of control programmatically in other parts of my code?

    I add the column like this:

    DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
    col.ReadOnly = false;
    col.Name = "Status";
    col.HeaderText = "Status";
    dgv.Columns.Add(col);
    

    All the cells in that column now will have a textbox. I can get the control as a textbox like this:

    private void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (dgv.CurrentCell.ColumnIndex == 5 && e.Control is TextBox)
        {
             //something
        }
    }
    

    How do I get the type of control hosted in the cell elsewhere? How to get e.Control from other parts of code so that I can do things like:

    ((TextBox)dgv[i, j].EditinControl).AutoCompleteSource = AutoCompleteSource.CustomSource;
    ((TextBox)dgv[i, j].EditinControl).AutoCompleteCustomSource = someSource;
    ((TextBox)dgv[i, j].EditinControl).AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    

    etc. What can substitute for EditinControl in the above line.. ??

  • nawfal
    nawfal over 12 years
    you seem to be an expert on datagridviews :)
  • Igby Largeman
    Igby Largeman over 12 years
    Nah, my knowledge is intermediate at best. Just stumbling along like most people. :)