Ver Mensaje Individual
  #4 (permalink)  
Antiguo 14/06/2010, 08:41
Ka0stj
 
Fecha de Ingreso: febrero-2010
Ubicación: México
Mensajes: 738
Antigüedad: 15 años, 1 mes
Puntos: 37
Respuesta: enlazando un datagridview con textbox

Primero cargo el DataGridView, para ello utilizo el siguiente método donde le especifico el query con el cual lo cargaré y el nombre del datagrid.

Código vb.net:
Ver original
  1. Public Sub Cargar_DatagridView(ByVal Instruccion_Sql As String, ByVal dgv As DataGridView)
  2.  
  3.         objConn = New SqlConnection(miconexion.StrConexion)
  4.         Dim MiDataReader As System.Data.SqlClient.SqlDataReader
  5.         Dim MiDataTable As New DataTable
  6.  
  7.         'Reseteo el Dataset para no mostrar repetidos
  8.         Try
  9.             ' SE ABRE LA CONEXION
  10.             objConn.Open()
  11.  
  12.             Dim sql As System.Data.SqlClient.SqlCommand = New SqlCommand(Instruccion_Sql, objConn)
  13.  
  14.             MiDataReader = sql.ExecuteReader
  15.  
  16.             MiDataTable.Load(MiDataReader)
  17.  
  18.             dgv.DataSource = MiDataTable
  19.  
  20.             objConn.Close()
  21.  
  22.         Catch ex As Exception
  23.             MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
  24.             objConn.Close()
  25.         End Try
  26.  
  27.     End Sub

Después para tomar los datos a los textbox con el evento cellcontentClick, cellclick o selectionchanged del datagridview de la siguiente forma:

Código vb.net:
Ver original
  1. Dim renglon As Integer = Me.DataGridView.CurrentCell.RowIndex
  2.  
  3. Me.Textbox1.Text = Me.DataGridView.Item(0,renglon).Value
  4. Me.Textbox2.Text = Me.DataGridView.Item(1,renglon).Value
  5. ...

donde 0,1,2, etc. son las columnas de mi renglon seleccionado. Saludos...