3

I am trying to convert a DataGridView cell to a control:

Control dat = new Control();
dat = Convert.ChangeType(dataGridView1[colIndex,rowIndex], typeof(Control));

I am fetching the values of colIndex and rowIndes from a index code. The problem is even though I tried many codes to convert, it does not work.

The exception I am getting is:

Can not implicitly convert an object to a control. An explicit convertion exist(are u missing a cast?)

David Hall
  • 32,624
  • 10
  • 90
  • 127
Vincent
  • 484
  • 4
  • 6
  • 21
  • Why exactly do you need to convert the cell to a control? A little context might help to give you an answer to your actual problem (since what you are asking to do is generally not possible beyond the EditingControl property). – David Hall Nov 21 '11 at 15:53

3 Answers3

6

To access the control that is hosted by a DataGridViewCell you use the EditingControl property of the cell when the cell is in editing mode.

This property returns a System.Windows.Forms.Control.

You can also get at the control within the DataGridViewEditingControlShowing event - the Control property of DataGridViewEditingControlShowingEventArgs is of type System.Windows.Forms.Control.

private void dataGridView1_EditingControlShowing(object sender, 
    DataGridViewEditingControlShowingEventArgs e)
{
    Control c = e.Control;
}

If you want to access the control at other times then (I believe) you are out of luck - I say that based mainly on this quote from the MSDN docs on DataGridViewEditingControlShowing:

The DataGridView control hosts one editing control at a time, and reuses the editing control whenever the cell type does not change between edits.

DSM
  • 342,061
  • 65
  • 592
  • 494
David Hall
  • 32,624
  • 10
  • 90
  • 127
  • Thanks all for ur replies... i am trying to check the error in datagrid entry and display a bubble message at the position of the error cell in datagrid that is why i am trying to convert the cell to control... from the above answer u ppl have given it shows it is not possible.. is it right... i think i have to use a message box then a buuble message.. any how thanks to all – Vincent Nov 21 '11 at 16:23
  • @vince - have you looked at the datagridview error text property? This is the out of the box feature designed to do exactly what you are asking for. – David Hall Nov 21 '11 at 17:52
  • i used ur idea and the above code.. to get what i was asking for... but it can not be done for checking correct data entered in textbox.. any how but i can show bubble message at that particular cell when incorrect value is being entered....thanks – Vincent Nov 23 '11 at 12:20
  • @vince do you mean checking the data entered into a DataGridViewTextBoxColumn cell as it is typed? – David Hall Nov 23 '11 at 12:22
1

Convert.ChangeType won't work unless there is a conversion defined between an Object and a Control, which of course there isn't. ChangeType is used most often for primitive types like integers and floats.

If dataGridView1[colIndex, rowIndex] is a Control, you should be able to use ordinary explicit casting: dat = (Control) dataGridView1[colIndex, rowIndex]

neontapir
  • 4,698
  • 3
  • 37
  • 52
1

The Item property of a DataGridView (this is what you access using row and column indexes) is of type DataGridViewCell, which is not inherited from System.Windows.Forms.Control.

kol
  • 27,881
  • 12
  • 83
  • 120
  • Reference to the DataGridViewCell class: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.aspx – CAbbott Nov 21 '11 at 15:52