DataGridViewImageCell

I am trying to get the information from a windows form button cell clicked on a new DataGridView control. The button cell is bound to a datagrid with the record ID keyfield bound to it. Clicking the button would produce the chosen key for further operations. The code example I have is from MSDN:

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.cellclick.aspx

My code as I used in my application as follows contains a cast which produces an invalid cast error. The example from msdn also produces a cast error. Does anyone have an example which works

private void dGVHistoryChooser_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

DataGridViewImageCell cell = (DataGridViewImageCell)

dGVHistoryChooser.Rows[e.RowIndex].Cells[e.ColumnIndex];

// More Stuff

// .

// .

}



Answer this question

DataGridViewImageCell

  • Alan Alvarez

    Never mind I figured it out. The example was using an ImageCell, I had a ButtonCell. Here is the working code. "mCustomerHistoryCurrentBO" is my business object for Customer History. I also had to set the button text off from the display value in order to get the object to contain the actual bound text from the table. This is a pain, but not insurmountable. There should be a property like "BoundValue" or something.

    private void dGVHistoryChooser_CellContentClick(object sender, DataGridViewCellEventArgs e)

    {

    DataGridViewButtonCell cell = (DataGridViewButtonCell)

    dGVHistoryChooser.Rows[e.RowIndex].Cells[e.ColumnIndex];

    cell.UseColumnTextForButtonValue = false;

    mCustomerHistoryCurrentBO.Get(Convert.ToInt32(cell.Value));

    }


  • DataGridViewImageCell