簡體   English   中英

DataGridView.CellLeave無法識別單元格內容

[英]DataGridView.CellLeave not recognizing cell contents

我希望在滿足某些條件時(大多數情況下不是空的)用列1和3的組合內容更新第4列的內容。

請注意,如果我單擊和關閉一個完整的單元格,它將按預期工作。 好像CellLeave事件不知道最終內容。 第一次正確捕獲了來自Cell(0)的值。 不是Cell(2),也就是當前列。

這是我得到的:

Private Sub DataGridView2_CellLeave(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView2.CellLeave

    'Dim CurrentColumnIndex As Integer = DataGridView2.CurrentCell.ColumnIndex
    'Dim CurrentRowIndex As Integer = DataGridView2.CurrentCell.RowIndex

    If e.ColumnIndex = 2 Then
        If DataGridView2.Rows(e.RowIndex).Cells(1).Value <> "" Then
            DataGridView2.Rows(e.RowIndex).Cells(3).Value = DataGridView2.Rows(e.RowIndex).Cells(0).Value & "-" & DataGridView2.Rows(e.RowIndex).Cells(2).Value
        End If
    End If

End Sub

我願意接受其他方法,但是我已經完成了這一方法,因此替代方法應該很簡單。

我想要的結果:

+---+----+-----+       +---+---+-----+
| 1 | a  |     |  ---> | 1 | a | 1-a |
+---+----+-----+       +---+---+-----+

我的問題:

+---+----+-----+       +---+---+-----+      +---+---+-----+
| 1 | a  |     |  ---> | 1 | a | 1-  | ---> | 1 | a | 1-a |
+---+----+-----+       +---+---+-----+      +---+---+-----+

第一步是我輸入的東西,第二步是我離開牢房時發生的事情,第三步是我重新進入並離開牢房時發生的事情。

當然,由於尚未提交更改,因此您不會在CellLeave事件中獲得更改的值。 在您的情況下,您將在第三步中獲得該值,因為更改后的值將在此時提交。

如果要獲取更改的值,請訂閱CellValueChanged事件。

dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

在一個示例程序中,我將單元格值從1更改為3。查看如何觸發事件以及值發生變化。

  1. 細胞離開-1
  2. 細胞驗證-1
  3. CellValueChanged-3
  4. CellValidated-3
  5. CellEndEdit-3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM