2014-01-07 108 views
0

我想清除單元格,如果某些條件得到滿足。但它不工作。如何以編程方式設置DataGridView單元格的值?

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 
+2

弗洛伊德博士承認你的標題:) – thumbmunkeys

+0

「它的不工作」究竟意味着什麼?你有什麼錯誤嗎?它在某些條件下不工作/從不??等 – varocarbas

+0

它不會給我任何錯誤..它只是沒有工作。決不。 – Arbaaz

回答

1

這很容易,只需通過的rowIndexColumnIndex

如。

Dim rowId = 0 
Dim colId = 1 

DataGridView1(colId, rowId).Value = "HELLO" 

所以,你的情況...像

If ... Then 
    DataGridView1(e.ColumnIndex, e.RowIndex).Value = "" 
End If 
+0

請檢查我的更新...它的工作原理,但它只有當我回到細胞,然後再離開時纔有效 – Arbaaz

+0

@Arbaaz你爲什麼要處理CellLeave事件? – Vland

+0

那麼我應該使用什麼事件? – Arbaaz

0

我不得不用B_LASPAC.EditedFormattedValue.ToString(),而不是B_LASPAC.Value.ToString()獲得對細胞離開細胞的價值,還我意識到我需要通過使用DataGridView1.EndEdit()結束的DataGridView的編輯模式之前我設置單元格的新值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 
相關問題