2014-02-26 62 views

回答

0

你在這裏。這將顯示您單擊的行上的第一個選定值。

Dim rowIndex As Integer 
Dim selectedItem As String 
rowIndex = yourdatagridview.CurrentCell.RowIndex() 
selectedItem = yourdatagridview.Item(0, rowIndex).Value.ToString 
MsgBox(selectedItem) 
0

嘗試這樣

Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) 
    If InlineAssignHelper(DataGridView1.SelectedRows.Count, 0) Then 
     TextBox1.Value = DataGridView1.SelectedRows(0).Cells(0).Value 
     TextBox2.Value = DataGridView1.SelectedRows(0).Cells(1).Value 
     TextBox3.Value = DataGridView1.SelectedRows(0).Cells(2).Value 
     TextBox4.Value = DataGridView1.SelectedRows(0).Cells(3).Value 
    End If 
End Sub 

(OR)

Private Sub dataGridView1_MouseClick(sender As Object, e As MouseEventArgs) 
    Dim dr As DataGridViewRow = dataGridView1.SelectedRows(0) 
    textBox1.Text = dr.Cells(0).Value.ToString() 
    ' or simply use column name instead of index 
    'dr.Cells["id"].Value.ToString(); 
    textBox2.Text = dr.Cells(1).Value.ToString() 
    textBox3.Text = dr.Cells(2).Value.ToString() 
    textBox4.Text = dr.Cells(3).Value.ToString() 
End Sub 

,並添加下面一行在你的加載事件

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect 
0

你的問題有點vauge但如果我理解它正確,你想顯示val當您在其他控件中單擊某個單元格(或該行中第一個單元格的值)。

在這種情況下,使用數據網格視圖的事件處理程序,例如:

Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick 
    'for clicked cell 
    Textbox1.Text = DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value 
    'for first cell 
    Textbox1.Text = DataGridView1.Item(0, e.RowIndex).Value 
End Sub