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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23943398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:37:53  来源:igfitidea点击:

How do I change the value of a specific DataGridView Cell?

vb.netvisual-studio-2012datagridview

提问by alexanderd5398

I've been trying to add a feature to my program that allows you to choose a file using OpenFileDialog (when you double click on a DataGridView cell) and change the value of that cell to the path of the file you chose. I have no idea what the command is to do that. I've tried to guess and I looked up on the internet and couldn't find anything.

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

回答by SSS

I'd use CellClickrather than CellContentClickas follows...

我会使用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