Domanda

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.. ??

È stato utile?

Soluzione

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; 
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top