如何更改特定DataGridView单元格的值?

时间:2014-05-29 21:34:41

标签: vb.net visual-studio-2012 datagridview

我一直试图在我的程序中添加一个功能,允许您使用OpenFileDialog选择文件(双击DataGridView单元格时)并将该单元格的值更改为文件的路径选择。我不知道命令是做什么的。我试图猜测,我在网上查了一下,找不到任何东西。

Private Sub dgSound_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgSound.CellDoubleClick

    If e.ColumnIndex = 3 Then 'Index of "file" column
        Dim file As String = ""
        OpenFileDialog1.ShowDialog()
        file = OpenFileDialog1.FileName

        'Contents of cell that was clicked = file          

    End If


End Sub

1 个答案:

答案 0 :(得分:1)

我使用CellClick而不是CellContentClick,如下所示......

  Private Sub dgSound_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgSound.CellClick
    If e.ColumnIndex = 3 Then 'Index of "file" column
      Dim file As String = ""
      OpenFileDialog1.ShowDialog()
      file = OpenFileDialog1.FileName

      'Contents of cell that was clicked = file          
      dgSound.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = file

    End If

  End Sub