【问题标题】:How do I change the value of a specific DataGridView Cell?如何更改特定 DataGridView 单元格的值?
【发布时间】:2014-05-29 21:34:40
【问题描述】:

我一直在尝试向我的程序添加一项功能,该功能允许您使用 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

【问题讨论】:

  • e 传递给事件,其中包含一些行/列信息来判断哪个被点击。
  • @Plutonix 我知道,但我不知道更改该单元格的具体命令。
  • dgSound(e.ColumnIndex, e.RowIndex).Value = "Whatever Goes Here"
  • @Plutonix 谢谢!我没想到会那么容易。

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


【解决方案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

【讨论】:

  • 我没有使用 CellContentClick,我使用的是 CellDoubleClick。感谢您的回答!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多