简体   繁体   English

从另一个表单中删除DataGridView的行

[英]Remove row of DataGridView from another form

I have problem with removing row from DataGridView. 我在从DataGridView中删除行时遇到问题。 I have DataGridView with clients. 我有带客户端的DataGridView。 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: 在此表单中,有一个“删除”按钮,其中单击“ Sub”是此代码:

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. 我以与datagridview相同的形式尝试了此代码,并且有效。 I would be very grateful for your help. 谢谢您的帮助。

The way you are accessing DataGridView1 would only work if DataGridView1 is shared. 仅当共享DataGridView1时,您访问DataGridView1的方式才有效。 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. 在您的客户端数据表单上,创建一个构造函数,该构造函数可以采用DataGridViewRows或DataGridView的集合,这样便可以对其进行访问。

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. 这样,您可以直接访问datagridview。

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 : 单击行时启动的第二种形式必须使用withevents声明:

    Dim WithEvents secondForm As ClientForm

  3. An event handler that will catch the row being deleted and remove it from the datagrid view 一个事件处理程序,它将捕获要删除的行并将其从datagrid视图中删除

     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调用。

    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. 每次单击“删除”按钮后,所有准备就绪,将触发该事件并被称为辅助表单的表单捕获,并应删除该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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