Este código en VB muestra los datos de una tabla en un DataGridView y al dar click sobre la celda llena los valores en los respectivos textbox, fácil.
Imports
System.Data
Imports
System.Data.SqlClient
Imports
System.Configuration
Public
Class Form1
Dim sql As String = ConfigurationManager.ConnectionStrings("con").ConnectionString.ToString
Dim
consulta = "select * from img"
Dim da As SqlDataAdapter
Dim ds As DataSet
Dim conn As SqlConnection
Private Sub Form1_Load(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
MyBase.Load
Using
conn As New SqlConnection(sql)
Using
da As New SqlDataAdapter(consulta, sql)
Using
ds As New DataSet
da.Fill(ds, "datos")
DataGridView1.DataSource =
ds
DataGridView1.DataMember = "datos"
End
Using
End
Using
End Using
End Sub
Private Sub DataGridView1_CellClick(ByVal
sender As Object,
ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs)
Handles DataGridView1.CellClick
TextBox1.Text =
DataGridView1.Rows(e.RowIndex).Cells(0).Value
TextBox2.Text =
DataGridView1.Rows(e.RowIndex).Cells(1).Value
TextBox3.Text =
DataGridView1.Rows(e.RowIndex).Cells(2).Value
End Sub
End Class
y en C# quedaría así:
using
Microsoft.VisualBasic;
using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Data;
using
System.Diagnostics;
using
System.Data.SqlClient;
using
System.Configuration;
Public
Class Form1
{
string
sql =
ConfigurationManager.ConnectionStrings("con").ConnectionString.ToString;
consulta = "select * from img";
SqlDataAdapter da;
DataSet ds;
SqlConnection conn;
private
void Form1_Load(System.Object sender, System.EventArgs e)
{
using
(SqlConnection conn = new SqlConnection(sql)) {
using
(SqlDataAdapter da = new SqlDataAdapter(consulta,
sql)) {
using (DataSet ds = new
DataSet()) {
da.Fill(ds,
"datos");
DataGridView1.DataSource
= ds;
DataGridView1.DataMember
= "datos";
}
}
}
}
private
void DataGridView1_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs
e)
{
TextBox1.Text =
DataGridView1.Rows(e.RowIndex).Cells(0).Value;
TextBox2.Text =
DataGridView1.Rows(e.RowIndex).Cells(1).Value;
TextBox3.Text =
DataGridView1.Rows(e.RowIndex).Cells(2).Value;
}
Public
Form1()
{
Load += Form1_Load;
}
}