单击按钮时删除数据网格视图中的行
我有一个 form1
和 form2
..
form1:我有一个带有按钮列的 datagridview。当我单击任何行的按钮单元格时,相应的行值将传输到 form2。那工作得很好。
表单 2:通过使用 setter 和 getter,我从 form1 获取了值,并在表单 2 的文本框中表示了值,这很好。
我在表单 2 中有一个复选框。当我单击该复选框时,我需要删除表单 1 中数据网格视图中的行(当我单击按钮单元格时,其行值将传输到表单 2。该行将被删除来自 form1 中的数据网格视图)。
我该如何解决这个问题?
I have a form1
and form2
..
form1: I have a datagridview with a button column. When I click on the button cell for any row the corresponding row values will be transferred to form2. That was working fine.
Form 2: by using setters and getters I have got the values from form1 and I represent the values in textboxes in form 2, and that was fine.
I have a checkbox in form 2. When I click on the check box, I need to remove the row in datagrid view in form 1 (whose row values are transferred to form 2 when I click on the button cell. That row will be deleted from the datagrid view in form1).
How do I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您将数据从
Form1
传递到Form2
时,还要传递行索引。它将帮助您轻松删除该行。标记 Form1 的
DataGridView
public
或将其作为构造函数参数传递给Form2
,然后调用DataGridView1.Rows.RemoveAt(rowIndex );
删除由rowIndex
指定的索引处的行。When you pass data from
Form1
toForm2
, also pass the row index. It will help you delete the row easily.Either mark the
DataGridView
of Form1public
or pass it as a constructor parameter toForm2
and then callDataGridView1.Rows.RemoveAt(rowIndex);
to delete row at the index specified byrowIndex
.如果您知道如何从同一表单的网格视图中删除记录,这很容易。
您可以按照与 StackOverflow 问题的答案相同的方式以一种形式更新网格视图。
您可以传递记录 ID 作为事件参数。
您可以使用
RemoveAt
方法从网格视图中删除记录。但是,如果您共享数据源并将其从表单 2 中删除,则需要再次绑定数据才能从网格中删除。 (请参阅如何:实现属性更改通知关于如何更新gridview。)
如果在form1上直接从gridview中删除它,则不需要再次绑定它。
If you know how to delete a record from a grid view from the same form, this is easy.
You can follow the same way as in the answer of Stack Overflow question Updating the gridview in one form.
As the event arguments you can pass the record ID.
You can use the
RemoveAt
method for deleting a record from the grid view.But if you share the datasource and remove it from form 2 you need to bind data again to remove from the grid. (See How to: Implement Property Change Notification on how to update the gridview.)
If you delete it on the form1 and directly from the gridview, you don't need to bind it again.