[英]How to set value of a DataGridView Cell programatically?
如果滿足某些條件,我將嘗試清除單元格。 但是它不起作用。
UPDATE
Private Sub DataGridView1_CellLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
Dim laspac As Int16 = DataGridView1.Columns("LASPAC").Index
If e.ColumnIndex = laspac And DataGridView1.Rows(e.RowIndex).Cells("CType").Value.ToString.Trim() = "B" Then
Dim B_LASPAC As DataGridViewCell = DataGridView1.Rows(e.RowIndex).Cells("LASPAC")
Dim A_LASPAC As DataGridViewCell = DataGridView1.Rows(e.RowIndex - 1).Cells("LASPAC")
If B_LASPAC.Value.ToString.Trim.Length <> 0 Then
If Convert.ToDecimal(B_LASPAC.Value) + Convert.ToDecimal(A_LASPAC.Value) > 0 Then
B_LASPAC.Value = ""
End If
End If
End If
End Sub
非常簡單,只需傳遞RowIndex和ColumnIndex
例如。
Dim rowId = 0
Dim colId = 1
DataGridView1(colId, rowId).Value = "HELLO"
所以,就您而言...
If ... Then
DataGridView1(e.ColumnIndex, e.RowIndex).Value = ""
End If
我必須使用B_LASPAC.EditedFormattedValue.ToString()
而不是B_LASPAC.Value.ToString()
才能在離開單元格時獲取單元格的值,而且我意識到我需要使用DataGridView1.EndEdit()
結束DataGridView的EditMode。 DataGridView1.EndEdit()
,在我設置單元格B_LASPAC.Value = ""
的新值之前
Private Sub DataGridView1_CellLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
Dim laspac As Int16 = DataGridView1.Columns("LASPAC").Index
If e.ColumnIndex = laspac And DataGridView1.Rows(e.RowIndex).Cells("CType").Value.ToString.Trim() = "B" Then
Dim B_LASPAC As DataGridViewCell = DataGridView1.Rows(e.RowIndex).Cells("LASPAC")
Dim A_LASPAC As DataGridViewCell = DataGridView1.Rows(e.RowIndex - 1).Cells("LASPAC")
Dim str As String = DataGridView1.Rows(e.RowIndex).Cells("LASPAC").EditedFormattedValue.ToString()
If B_LASPAC.EditedFormattedValue.ToString.Trim.Length <> 0 Then
If Convert.ToDecimal(B_LASPAC.EditedFormattedValue.ToString()) + Convert.ToDecimal(A_LASPAC.EditedFormattedValue.ToString()) > 0 Then
DataGridView1.EndEdit()
B_LASPAC.Value = ""
End If
End If
End If
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.