当单击按钮时,我试图触发此命令
 Private Sub ClickDataGridview(sender As Object, e As DataGridViewCellMouseEventArgs)
    If e.RowIndex >= 0 Then
        Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
        TextBox1.Text = row.Cells(0).Value.ToString
        TextBox2.Text = row.Cells(1).Value.ToString
    End If
End Sub
 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ClickDataGridview()
    End Sub但遗憾的是我收到了两个错误
未为“私有子DataGridViewCellMouseEventArgs)'.
)
我应该把它写成要工作的if语句吗?或者我是否应该尝试其他方法来触发这个事件?
发布于 2021-02-22 05:10:28
有一种方法,但你必须小心选择细胞。如果您只需要执行行操作,那么这是可以的。我建议不要这样做,而是在网格视图中为每一行放置按钮并执行操作
 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Try
 If DataGridView1.SelectedCells.Count > 0 Then
'Here you can change Datagridview row selection property and get selectedrows instead of selected cells
            Dim i_rowindex As Integer = DataGridView1.SelectedCells(0).RowIndex
            Dim i_colIndex As Integer = DataGridView1.SelectedCells(0).ColumnIndex
            DataGridView1_CellMouseClick(sender, New DataGridViewCellMouseEventArgs(i_colIndex, i_rowindex, 0, 0, New MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0)))
End
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub发布于 2021-02-22 05:55:23
我可能遗漏了什么,但是…我不明白你为什么要强迫用户“点击”一个按钮来设置文本框。如果连接网格“SelectionChanged”事件并更新该事件中的文本框,则用户不必单击按钮或单元格。如果用户使用箭头键,输入键,Tab键,甚至“单击”单元格,文本框将自动更改,用户不必单击按钮。
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
  If (DataGridView1.CurrentRow IsNot Nothing) Then
    TextBox1.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString()
    TextBox2.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString
  End If
End Sub或者..。最好还是..。如果网格使用数据源,则只需将文本框“绑定”到该“相同”数据源。您将不必连接任何网格事件。
TextBox1.DataBindings.Add(New Binding("Text", datasource, "datasourceColumnName1"))
TextBox2.DataBindings.Add(New Binding("Text", datasource, "datasourceColumnName2"))发布于 2021-02-22 06:17:59
正如我可能已经提到的一两次,不要直接调用事件处理程序。将公共代码放入自己的方法中,然后酌情从每个事件处理程序中调用该方法。在这种情况下:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    GetRowValues(DataGridView1.CurrentRow)
End Sub
Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
    If e.Button = MouseButtons.Left AndAlso e.RowIndex >= 0 Then
        GetRowValues(DataGridView1.Rows(e.RowIndex))
    End If
End Sub
Private Sub GetRowValues(row As DataGridViewRow)
    TextBox1.Text = row.Cells(0).Value.ToString()
    TextBox2.Text = row.Cells(1).Value.ToString()
End Subhttps://stackoverflow.com/questions/66309982
复制相似问题