-2

I have a datagrid in Windows Forms and there are 3 columns. One is a button type and should be clickable and that does work. However, if I mistakenly click on the other 2 columns, I get an exception error as stated below. I understand the error because my intent was to cast the clicked cell Button to a local variable of the same type. What I want to know is how to prevent the exception when I click on the other two columns.

Here is the code:

         private: System::Void dataGridView19_CellContentClick(System::Object^ sender, 
         System::Windows::Forms::DataGridViewCellEventArgs^ e) {
           DataGridViewButtonCell^ cell = (DataGridViewButtonCell^)dataGridView19->CurrentCell;
           Console::WriteLine("Speaking ");
           SimVoice[Convert::ToInt16(dataGridView19->CurrentRow->Cells["dataGridViewTextBoxColumn33"]->Value)]->speak();

      };

And here is the error:

>     System.InvalidCastException: Unable to cast object of type 
      'System.Windows.Forms.DataGridViewTextBoxCell' to type 
      'System.Windows.Forms.DataGridViewButtonCell'.
       at CppCLRWinformsProjekt.Form1.dataGridView19_CellContentClick(Object sender, 
      DataGridViewCellEventArgs e) in F:\WindowsProjects\CppCLR_WinformsP3-V1 - 
      7\CppCLR_WinformsProjekt3\Form1.h:line 2785
        at 
       System.Windows.Forms.DataGridView.OnCellContentClick(DataGridViewCellEventArgs e)
   at System.Windows.Forms.DataGridView.OnCommonCellContentClick(Int32 columnIndex, Int32 rowIndex, Boolean doubleClick)
   at 
  
  
 System.Windows.Forms.DataGridViewCell.OnMouseUpInternal(DataGridViewCellMouseEventArgs e)
   at System.Windows.Forms.DataGridView.OnCellMouseUp(DataGridViewCellMouseEventArgs e)
   at System.Windows.Forms.DataGridView.OnMouseUp(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Thanks, Chris

Chris
  • 67
  • 5
  • 2
    Test if it's the right type before you do the cast. I don't know the CLI syntax, but the modern C# way would be `if (dataGridView19.CurrentCell is DataGridViewButtonCell btn) {btn.DoSomething(); }`. You probably want to tag this question `winforms` to get more exposure to WinForms folks – Flydog57 Jun 03 '21 at 22:53
  • In the grids `CellContentClick` the passed in variable … `DataGridViewCellEventArgs e` has a property to identify “which” column was clicked… `e.ColumnIndex` … Check this value before you cast the cell, IF `e.ColumnIndex` = the button column, then the cast will be successful. If `e.ColumnIndex` != the button column then do not cast the cell and simply ignore it. – JohnG Jun 04 '21 at 03:23

1 Answers1

1

From: How to handle click event in Button Column in Datagridview?

I think this is the answer. Let me know if there is a better way.

if (e->ColumnIndex == dataGridView19->Columns["dataGridViewButtonColumn7"]->Index && e->RowIndex >= 0) {
        DataGridViewButtonCell^ cell = (DataGridViewButtonCell^)dataGridView19->CurrentCell;
        Console::WriteLine("Speaking ");
        SimVoice[Convert::ToInt16(dataGridView19->CurrentRow->Cells["dataGridViewTextBoxColumn33"]->Value)]->speak();

        //Console::WriteLine("Button on row {0} clicked", e->RowIndex);
    }
Chris
  • 67
  • 5