Cara Menampilkan Data di Textbox Dari GridView

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Conn.ConnectionString = Connection1.ConnectionString
query = “select * from [pengguna]”
query2 = “select nama, alamat, no_telepon, role, tgl_masuk from [pengguna] where kode_pengguna='” & kodeEdit.SelectedValue.ToString() & “‘”
query3 = “select top(1) * from [pengguna]”
Dim cmd As New SqlCommand(query, Conn)
Dim cmd2 As New SqlCommand(query2, Conn)
Dim cmd3 As New SqlCommand(query3, Conn)
Dim tabel As New DataTable
Conn.Open()
dataReader = cmd.ExecuteReader()
tabel.Load(dataReader)
GridView1.DataSource = tabel
GridView1.DataBind()
dataReader3 = cmd3.ExecuteReader()
If dataReader3.Read() Then
namaEdit.Text = dataReader3(“nama”).ToString()
alamatEdit.Text = dataReader3(“alamat”).ToString()
tlpEdit.Text = dataReader3(“no_telepon”).ToString()
tglEdit.Text = Left(dataReader3(“tgl_masuk”).ToString(), 2)
blnEdit.Text = Mid(dataReader3(“tgl_masuk”).ToString(), 4, 2)
thnEdit.Text = Right(dataReader3(“tgl_masuk”).ToString(), 4)
roleEdit.Text = dataReader3(“role”).ToString()
End If
dataReader3.Close()
dataReader2 = cmd2.ExecuteReader()
If dataReader2.Read() Then
namaEdit.Text = dataReader2(“nama”).ToString()
alamatEdit.Text = dataReader2(“alamat”).ToString()
tlpEdit.Text = dataReader2(“no_telepon”).ToString()
tglEdit.Text = Left(dataReader2(“tgl_masuk”).ToString(), 2)
blnEdit.Text = Mid(dataReader2(“tgl_masuk”).ToString(), 4, 2)
thnEdit.Text = Right(dataReader2(“tgl_masuk”).ToString(), 4)
roleEdit.Text = dataReader2(“role”).ToString()
End If
dataReader.Close()
dataReader2.Close()
Conn.Close()
End Sub

Kemaren ini sempet bingung saat ingin mengoding untuk update di ASP.NET karena tidak tahu cara menampilkan data dari gridview ke textbox. Misal, ketika memilih id_user dari combobox, maka data seperti nama, alamat, telepon, email dan lainnya dapat ditampilkan di textbox yang sudah disediakan.

contoh

contoh

saat meload halaman tambahkan coding berikut :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Conn.ConnectionString = Connection1.ConnectionString

//query untuk gridview

query = “select * from [pengguna]”

//query untuk mengambil data sesuai dengan pilihan di combobox

query2 = “select nama, alamat, no_telepon, role, tgl_masuk from [pengguna] where kode_pengguna='” & kodeEdit.SelectedValue.ToString() & “‘”

//untuk menampilkan data pertama yang ada di combobox

query3 = “select top(1) * from [pengguna]”

Dim cmd As New SqlCommand(query, Conn)

Dim cmd2 As New SqlCommand(query2, Conn)

Dim cmd3 As New SqlCommand(query3, Conn)

Dim tabel As New DataTable

Conn.Open()

dataReader = cmd.ExecuteReader()

tabel.Load(dataReader)

GridView1.DataSource = tabel

GridView1.DataBind()

//ini untuk bagian memilih dari combobox

dataReader3 = cmd3.ExecuteReader()

//di cek isi dari query3

If dataReader3.Read() Then

//memasukkan data hasil query3 ke textbox

namaEdit.Text = dataReader3(“nama”).ToString()

alamatEdit.Text = dataReader3(“alamat”).ToString()

tlpEdit.Text = dataReader3(“no_telepon”).ToString()

tglEdit.Text = Left(dataReader3(“tgl_masuk”).ToString(), 2)

blnEdit.Text = Mid(dataReader3(“tgl_masuk”).ToString(), 4, 2)

thnEdit.Text = Right(dataReader3(“tgl_masuk”).ToString(), 4)

roleEdit.Text = dataReader3(“role”).ToString()

End If

dataReader3.Close()

//ini untuk menampilkan data pertama dari combobox saat di page load //pertama kali

dataReader2 = cmd2.ExecuteReader()

If dataReader2.Read() Then

namaEdit.Text = dataReader2(“nama”).ToString()

alamatEdit.Text = dataReader2(“alamat”).ToString()

tlpEdit.Text = dataReader2(“no_telepon”).ToString()

tglEdit.Text = Left(dataReader2(“tgl_masuk”).ToString(), 2)

blnEdit.Text = Mid(dataReader2(“tgl_masuk”).ToString(), 4, 2)

thnEdit.Text = Right(dataReader2(“tgl_masuk”).ToString(), 4)

roleEdit.Text = dataReader2(“role”).ToString()

End If

dataReader.Close()

dataReader2.Close()

Conn.Close()

End Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s