2014-07-21 33 views
0

當用戶單擊WinFormsDataGridView中的按鈕時是否可以禁止驗證?我正在使用DataGridViewButtonColumn作爲按鈕列。 VB.Net和.Net 4.0。單擊DataGridView列中的按鈕時禁止驗證


我有一列用戶輸入文件路徑和旁邊的按鈕列。用戶可以單擊這些按鈕來顯示一個通用對話框並瀏覽文件,並選擇的文件路徑將存儲回第一列。在CellValidating事件中,我驗證文件路徑是否存在。但是,當用戶單擊「瀏覽」按鈕選擇不同的文件時,應用此驗證並不便於用戶使用!

對於獨立按鈕,我將CausesValidation設置爲False。但我不想禁用整個DataGridView的驗證,只是按鈕。 DataGridViewButtonColumn我找不到合適的房產。

Private Sub mDataGridView_CellValidating(ByVal sender As Object, 
    ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) 
    Handles mDataGridView.CellValidating 

    Dim sValue As String = e.FormattedValue.ToString 

    If e.ColumnIndex = 0 Then 
    If Not IO.File.Exists(sValue) Then 
     MsgBox("The file does not exist.") 
     e.Cancel = True 
    End If 
    End If 
End Sub 

回答

0

社區沒有迴應,所以我更努力地工作,在this MSDN forum post上找到了自己的答案!

Private Sub mDataGridView_CellValidating(ByVal sender As Object, 
    ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) 
    Handles mDataGridView.CellValidating 

    Dim sValue As String = e.FormattedValue.ToString 

    If e.ColumnIndex = 0 Then 
    'Find current mouse cursor position 
    Dim p As Point = mDataGridView.PointToClient(Cursor.Position) 
    Dim hit As DataGridView.HitTestInfo = mDataGridView.HitTest(p.X, p.Y) 
    If (hit.Type = DataGridViewHitTestType.Cell) Then 
     'Mouse is over a cell 
     If hit.ColumnIndex = <column containing browse button> Then 
     If hit.RowIndex = e.RowIndex Then 
      'And it's a browse button cell for the same row. Abort validation here.    
      Exit Sub 
     End If 
     End If 
    End If 

    If Not IO.File.Exists(sValue) Then 
     MsgBox("The file does not exist.") 
     e.Cancel = True 
    End If 
    End If 
End Sub