如何使用 VB.Net 在 datagridview 中使用 CellEndEdit 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23842547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:36:07  来源:igfitidea点击:

How to use CellEndEdit event in datagridview using VB.Net?

vb.netvisual-studio-2010c#-4.0datagridview

提问by Malar

In the data grid,I have two columns A and B.

在数据网格中,我有两列 A 和 B。

I need only one column to be filled in a row.

我只需要在一行中填充一列。

In this,after filling column A,if i try to fill column B or vice-verse, I need the other column to be empty.

在此,在填充 A 列之后,如果我尝试填充 B 列或反之亦然,我需要另一列为空。

Note: I am filling the grid manually.

注意:我正在手动填充网格。

Please help me out of this and thanks in advance.

请帮我解决这个问题,并提前致谢。

采纳答案by LarsTech

The DataGridViewCellEventArgs parameter you get from that event tells you which column and row you are currently editing, so you can easily wipe out the other column with that information:

您从该事件中获得的 DataGridViewCellEventArgs 参数告诉您当前正在编辑的列和行,因此您可以轻松地用该信息清除另一列:

Private Sub dgv_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) _
                                              Handles dgv.CellEndEdit
  If e.ColumnIndex = 0 Then
    dgv.Rows(e.RowIndex).Cells(1).Value = String.Empty
  ElseIf e.ColumnIndex = 1 Then
    dgv.Rows(e.RowIndex).Cells(0).Value = String.Empty
  End If
End Sub

To do this when the user startsto edit the cell, simply use the CellBeginEdit event instead:

要在用户开始编辑单元格时执行此操作,只需使用 CellBeginEdit 事件:

Private Sub dgv_CellBeginEdit(sender As Object, _
                              e As DataGridViewCellCancelEventArgs) _
                              Handles dgv.CellBeginEdit
  If e.ColumnIndex = 0 Then
    dgv.Rows(e.RowIndex).Cells(1).Value = String.Empty
  ElseIf e.ColumnIndex = 1 Then
    dgv.Rows(e.RowIndex).Cells(0).Value = String.Empty
  End If
End Sub