|  | 
	                
  
    |  |  
    | 
        
        สอบถามแสดงค่าข้อมูลลง DataGridView โดยใช้ความสัมพันธ์ของข้อมูล ของ VB.Net ค่ะ     |  
    |  |  
 
              
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  
 Code (VB.NET)
 
 Public Class Form4
    Dim cnn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\PostCode.mdb;Persist Security Info=True;Jet OLEDB:Database Password=4410210091"
    Private Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.DataSource = GetTableOLEDB("SELECT * FROM Sett_Post_Province", cnn)
        DataGridView2.DataSource = GetTableOLEDB("SELECT * FROM Sett_Post_PostCode", cnn)
    End Sub
    Private Function GetTableOLEDB(SQL As String, strConn As String) As Data.DataTable
        Dim conn As New Data.OleDb.OleDbConnection(strConn)
        Dim ds As New System.Data.DataSet()
        conn.Open()
        Dim strQuery As String = SQL
        Dim adapter As New Data.OleDb.OleDbDataAdapter(strQuery, conn)
        adapter.Fill(ds)
        Return ds.Tables(0)
    End Function
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    End Sub
    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        DataGridView2.DataSource = GetTableOLEDB("SELECT * FROM Sett_Post_PostCode where Pnv =" & DataGridView1(0, e.RowIndex).Value, cnn)
    End Sub
End Class
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-02-12 11:04:21 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ขอบคุณค่ะ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-02-13 11:20:35 | By :
                            Preeyapan |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ใช้ได้ป่าวครับโค๊ดบ้านๆผม 
 พอดีไม่ค่อยถนัด SQL
    
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-02-13 11:36:34 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ยังงงยุเลยค่ะ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-02-13 11:59:28 | By :
                            Preeyapan |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  
 รันแล้ว error แบบนี้ค่ะ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-02-13 12:08:13 | By :
                            Preeyapan |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ไม่เป็นไรค่ะ ขอบคุณค่ะ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-02-13 15:27:38 | By :
                            Preeyapan |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Ms SQL Server 2008 
 Data.SqlClient.SqlConnection
 
 ครับพอดีตกไป
    
 แค่นั้นก็ใช้ได้แระครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-02-13 16:51:34 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              |  
 ข้อมูลยังไม่เชื่อมโยงกันค่ะ พอกดรัน รันได้ แต่พอจะกดขึ้นมูลก็เกิด error แบบนี้
 รบกวนด้วยนะค่ะ ขอบคุณค่ะ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-02-13 19:24:28 | By :
                            Preeyapan |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ต้องเอา SQL มาดูครับ เพราะมันบอกว่าไม่เจอ คอลัมชื่อนี้
 
 SQL ควรจะเป็น
 
 "select * form  ttttt where resub_id = " &  DataGridView1(0, e.RowIndex).Value
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-02-14 10:35:16 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | GetTableOLEDB นี่คืออะไรค่ะ งง
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-03-02 13:09:08 | By :
                            Preeyapan |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Code (C#) 
 Private Function GetTableOLEDB(SQL As String, strConn As String) As Data.DataTable
        Dim conn As New Data.OleDb.OleDbConnection(strConn)
        Dim ds As New System.Data.DataSet()
        conn.Open()
        Dim strQuery As String = SQL
        Dim adapter As New Data.OleDb.OleDbDataAdapter(strQuery, conn)
        adapter.Fill(ds)
        Return ds.Tables(0)
    End Function
 ไม่รู้ว่าศัพย์มันคืออะไรแต่ผมชอบเรียกมันว่า Function
 อย่าง
 Private Function GetTableOLEDB(SQL As String, strConn As String) As Data.DataTable
 
 มันจะคืนค่าออกมาเป็น Data.DataTable
 แล้วเราก็เอา Data.DataTable นั้นไปใช้งานได้เลย
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2015-03-02 14:20:15 | By :
                            lamaka.tor |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |  |