在datagrid显示的数据中,我想双击任一行,即取得该行的值,放入文本框text1,text2中,有何好办法实现?datagrid表:  id    name
  12     gg
  13     bb
  15     jj

解决方案 »

  1.   

    Private Sub DataGrid1_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
        Dim RowValue, ColValue
        RowValue = DataGrid1.RowContaining(Y)
        ColValue = DataGrid1.ColContaining(x)
        If RowValue >= 0 And RowValue <= DataGrid1.VisibleRows And _
            ColValue >= 0 And ColValue <= DataGrid1.VisibleCols Then
            DataGrid1.Row = RowValue
        End If
        
        Dim i As Long
        For i = 0 To DataGrid1.Columns.Count - 1
           If DataGrid1.Columns(i).Caption = "id" Then
              Text1.Text = DataGrid1.Columns(i).Value
              Exit For
           End If
        Next
        For i = 0 To DataGrid1.Columns.Count - 1
           If DataGrid1.Columns(i).Caption = "name" Then
              Text2.Text = DataGrid1.Columns(i).Value
              Exit For
           End If
        Next
    End Sub
      

  2.   

    如果DataGrid1用的数据库连接的数据,方法为text1.text=rs.field("id")
    text2.text=rs.field("name")