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

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

    #1

    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();
    }
    
    }
  • Ramk
    New Member
    • Nov 2008
    • 61

    #2
    You can use UserDeletingRow to see the content of the currently deleting row as below.
    Code:
    private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
    {
           MessageBox.Show(e.Row.ToString() + " is deleting");
           if(myfilter)
            e.Cancel = true;//Cancels the Row deletion.
    }

    Comment

    • bmerlover
      New Member
      • Jul 2007
      • 49

      #3
      Thank you very much, worked like a charm. 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)
       {			 
            MessageBox::Show(e->Row->Cells[0]->Value->ToString());
           do_something(e->Row->Cells[0]->Value->ToString());
      }
      Last edited by bmerlover; Dec 11 '08, 06:24 PM. Reason: added the eventhandler portion in the windows generated code portion to show the complete method thanks to RAMK

      Comment

      Working...