Help with DataGrid and MemberwiseClone() Visual C++ using .NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bmerlover
    New Member
    • Jul 2007
    • 49

    Help with DataGrid and MemberwiseClone() Visual C++ using .NET

    I am currently using Visual Studio 2008 Pro edition programming a windows form application using Visual C++.

    I am using datagrids, the user can populate fields with data. I am not using datasets because the information doesn't have to remain once the app has closed. When the user selects a row and deletes it with the "Delete" key on the keyboard, I want to trigger an event. I got it to this point but now I am having a problem because I want to know the contents of the fields that were deleted. So I am trying this e->MemberwiseClon e() to get the contents of the cells and save into "row" so that I can gain access to the cells. But unfortunately I get this error:

    error C3767: 'System::Object ::MemberwiseClo ne': candidate function(s) not accessible

    MemberwiseClone () is under protected, so I tried the #pragma make_public but I also got an error with that, although I'm not sure if I used that correctly or not.

    My ultimate goal is to find out the contents of the cells that were deleted. I don't know if I am taking the right approach but that is my goal. I am open to any suggestions and would greatly appreciate them. Thanks in advance.

    Code:
    private: System::Void dataGridView1_RowsRemoved(System::Object^  sender, System::Windows::Forms::DataGridViewRowsRemovedEventArgs^  e)
    {
         //to verify that the deletion is working correctly
         MessageBox::Show("row " + e->RowIndex.ToString() + " has been removed");
    
         DataGridViewRow row = (DataGridViewRow) e->MemberwiseClone();
    }
    
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    MemberwiseClone is protected, it must be called within the context of the class.

    #pragma make_public is about altering the accessibility of native type members (C++ structs and classes) when you are unable to change the headers they are defined in.

    I see no way in which casting DataGridViewRow sRemovedEventAr gs to DataGridViewRow can be valid.

    Comment

    • bmerlover
      New Member
      • Jul 2007
      • 49

      #3
      Thanks Banfa for the information, I looked into it. This is the solution I found with help.

      This is the final version, I actually needed one specific cell (column index 0) from the row that was being deleted, so hence this code works great.

      Code:
      //for people viewing this thread, you need to add an event handler in your Windows Form Designer generated code portion. Without it, this won't work.
      //Shown here:
      this->dataGridView1->UserDeletingRow += gcnew System::Windows::Forms::DataGridViewRowCancelEventHandler(this, &Form1::dataGridView1_UserDeletingRow);
      
      
      
      private: System::Void dataGridView1_UserDeletingRow(System::Object^  sender, System::Windows::Forms::DataGridViewRowCancelEventArgs ^  e)
       {	
            //specifying a colum index of 0 of the row being deleted		 
            MessageBox::Show(e->Row->Cells[0]->Value->ToString());
            //passing that value from column 0 and row being deleted to be processed elsewhere      
            do_something(e->Row->Cells[0]->Value->ToString());
      }

      Comment

      Working...