简体   繁体   中英

Remove row of DataGridView from another form

I have problem with removing row from DataGridView. I have DataGridView with clients. When I click on row the new form opens (with client data). In this form there is a 'delete' button wherein click Sub is this code:

Form1.DataGridView1.Rows().RemoveAt(_personIndex)

The problem is that this code doesn't work. I tried this code in the same form as datagridview and it worked. I would be very grateful for your help.

The way you are accessing DataGridView1 would only work if DataGridView1 is shared. On your client data form, create a constructor that can take a collection of DataGridViewRows or a DataGridView, that way you can have access to it.

You should have your second form that loads the customer information fire an event and have your main form handle the event. This way you can access the datagridview directly.

You will need four things in order to do this:

  1. an event on the second form declared as:

    Friend Event DeleteClient(ByRef rowID As Integer)

  2. Your second form that launches when a row is clicked will have to be declared withevents :

    Dim WithEvents secondForm As ClientForm

  3. An event handler that will catch the row being deleted and remove it from the datagrid view

     Private Sub DeleteClient(ByRef rowID As Integer) Handles clientform.DeleteClient DataGridViw1.Rows().RemoveAt(rowID) End Sub 
  4. A RaiseEvent call in your button click event for the delete button.

    RaiseEvent DeleteClient(idOfRowToBeRemoved)

Once all that is in place everytime you click the delete button the event should be fired and caught by the form that called the secondary form and the row should be removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM