Dim dt As New DataTable()
dt.Columns.Add("ID")
dt.Columns.Add("Name")
For index = 1 To 10
dt.Rows.Add(New String() {index, "Data" + index.ToString()})
Next
If dt.Rows.Count > 0 Then
DataGridView1.DataSource = dt
End If
Me.TextBox1.Text = dt.Rows(0)("ID")
DataGridView1.Rows(0).Selected = True
รบกวนถามอีกเรื่องครับ คือผมเขียนโปรแกรมดึงข้อมูลจากฐานข้อมูลเอา วันที่ และ id มาแสดงที่ gridview แล้วทำการเลือกข้อมูลใน gridview ตามวันที่ที่ต้องการ จากนั้นก็เอาค่า id ที่เลือกมาเป็นตัวอ้างอิงเพื่อดึงข้อมูลจากฐานข้อมูลมาแสดงใน textbox อีกที(ทำเป็นส่วน master) แต่ข้อมูลกลับไม่แสดงที่ textbox แต่ถ้าผมใช้ control listbox จะทำได้ตามตัวอย่าง code ด้านล่าง
Code (VB.NET)
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
If (bidingsrc Is Nothing) Then
Exit Sub
End If
bidingsrc.Position = ListBox1.SelectedIndex
End Sub
Private Sub GridControl1_Click(sender As Object, e As EventArgs) Handles GridControl1.Click
MessageBox.Show(GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "CODE").ToString())
End Sub
Imports MySql.Data.MySqlClient
Public Class frm_physic_edit
Private Sub txt_hn_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt_hn.KeyDown
If (e.KeyCode = Keys.Enter) Then
Dim strID = Me.txt_hn.Text
'คำสั่งเพิ่ม 0 ไว้ข้างหน้าให้ครบ 9 หลัก
strID = strID.Trim.PadLeft(9, "0")
txt_hn.Text = strID
Dim contxt As String = "c:\ProgramData\sn_tool.txt"
Dim line() As String = IO.File.ReadAllLines(contxt)
Dim host As String = line(0)
Dim user As String = line(1)
Dim pwd As String = line(2)
Dim db As String = line(3)
Dim con As MySqlConnection = New MySqlConnection()
con.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", host, user, pwd, db)
sql = "select intdate,person_deform_head_id as id " & _
"from person_deform_head " & _
"where hn='" & txt_hn.Text & "' order by intdate desc "
con.Open()
command = New MySqlCommand(sql, con)
adapter = New MySqlDataAdapter(command)
datast = New DataSet()
adapter.Fill(datast, "dint")
gc_int.DataSource = datast.Tables("dint")
End If
End Sub
'Private Sub hn_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt_hn.KeyDown
Private Sub gridview1_rowclick(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridView1.RowClick
intdate.Text = GridView1.GetFocusedRowCellValue("intdate").ToString
txt_id.Text = GridView1.GetFocusedRowCellValue("id")
showdetails()
End Sub
Private Sub gridview1_SelectionChanged(sender As Object, e As EventArgs) Handles GridView1.SelectionChanged
GridView1.GetFocusedDataSourceRowIndex()
End Sub
Private Sub showdetails()
If Not (datast.Tables("pdh") Is Nothing) Then
datast.Tables("pdh").Clear()
End If
sql = "select pid,hn,cid,intdate from person_deform_head where person_deform_head_id = '" & txt_id.Text & "'"
command.CommandText = sql
adapter.SelectCommand = command
adapter.Fill(datast, "pdh")
GridControl1.DataSource = datast.Tables("pdh")
End Sub
Private Sub updatebinding()
txt_hn.DataBindings.Clear()
txt_cid.DataBindings.Clear()
txt_id.DataBindings.Clear()
Dim contxt As String = "c:\ProgramData\sn_tool.txt"
'อ่านค่าใน sn_tool.txt เพิ่อเอาค่ามาแสดงใน textbox
Dim line() As String = IO.File.ReadAllLines(contxt)
Dim host As String = line(0)
Dim user As String = line(1)
Dim pwd As String = line(2)
Dim db As String = line(3)
Dim con As MySqlConnection = New MySqlConnection()
con.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", host, user, pwd, db)
For Each ctrl As Control In Me.Controls
ctrl.DataBindings.Clear()
If TypeOf ctrl Is TextBox Then
ctrl.Text = ""
End If
Next
pic.Image = Nothing
sql = "select pdh.hn,pdh.vn,pdh.cid,pdh.person_deform_head_id from person_deform_head pdh " & _
"left outer join patient pt on pdh.cid = pt.cid where person_deform_head_id = 1 "
con.Open()
command = New MySqlCommand(sql, con)
adapter = New MySqlDataAdapter(command)
datast = New DataSet
adapter.Fill(datast, "master")
bidingsrc = New BindingSource()
bidingsrc.DataSource = datast.Tables("master")
txt_hn.DataBindings.Add("Text", bidingsrc, "hn")
txt_cid.DataBindings.Add("text", bidingsrc, "cid")
'txt_id.DataBindings.Add("value", bidingsrc, "person_deformj_head_id")
txt_vn.DataBindings.Add("text", bidingsrc, "vn")
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.FocusedRowChanged
If (bidingsrc Is Nothing) Then
Exit Sub
End If
bidingsrc.Position = GridView1.GetVisibleIndex(0)
showdetails()
End Sub
End Class