ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 3757|ตอบกลับ: 0

[VB.NET] การ UnBound Data บน DataGridView เพื่อจัดรูปแบบการแสดงผลฟิลด์ข้อมูล

[คัดลอกลิงก์]

308

กระทู้

498

โพสต์

5973

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
5973



พอดีแอดมินแอดมินได้รับโจทย์จากลูกค้ามา ให้รวมข้อมูล 3 ฟิลด์เข้าหากันเพื่อแสดงผลในหลักเดียวกันของตารางกริด โดยจะประกอบด้วยฟิลด์ที่อยู่ (Address), อำเภอ (Amphur) และจังหวัด (Province) ดูๆแล้วก็ไม่น่าจะมีปัญหาอะไรเนาะ แต่ทว่าการออกแบบตารางข้อมูลของแอดมิน จะทำการแยกชื่อจังหวัดออกไปอยู่อีกตารางต่างหาก แล้วมันมีปัญหาตรงไหน? เพราะเราสามารถเอาฟิลด์ข้อมูลมาจัดเรียงต่อกันใน Query ก็ได้นี่ ก็เพราะเนื่องจากว่าจังหวัดที่เป็นกรุงเทพมหานคร ต้องใช้คำว่าเขตแทนอำเภอ และจะต้องไม่มีคำนำหน้าว่าจังหวัด เอาล่ะซิทีนี้บันเทิงกันเลย หากเราไปใช้เงื่อนไขใน Query ... แอดมินเลยได้จังหวะนำเสนอการ UnBound Data เพื่อมาช่วยแก้ปัญหาในเรื่องนี้ขอรับกระผม


โดยปกติเราก็มักจะใช้วิธีการ Bound Data กันอยู่บ่อยๆ ซึ่งบางคนก็อาจจะรู้ หรือบางคนก็แทบจะไม่รู้จักเลยซ่ะด้วยซ้ำ แอดมินขอนำเสนอด้วยโค้ดแทนคำอธิบายดีกว่าครับ ...

โค้ดในส่วนของการผูกฟิลด์ข้อมูลเอาไว้ให้กับ DataTable ...
  1.     ' / --------------------------------------------------------------------
  2.     ' / อ่านข้อมูลจากฟิลด์ที่ระบุหรือต้องการ เข้ามาเก็บไว้ยัง DataTable
  3.     Private Function GetDataTable() As DataTable
  4.         Dim strSQL As String = _
  5.             " SELECT tblCustomer.CustomerPK, tblCustomer.CustomerID, tblCustomer.CustomerName, " & _
  6.             " tblCustomer.Address, tblCustomer.Amphur, tblCustomer.ProvinceFK, tblProvince.ProvinceName, tblCustomer.PostCode " & _
  7.             " FROM tblCustomer INNER JOIN tblProvince ON tblCustomer.ProvinceFK = tblProvince.ProvincePK " & _
  8.             " ORDER BY CustomerPK "
  9.         '/
  10.         Dim tbl As New DataTable
  11.         '// เพิ่มหลักเข้าสู่ DataTable
  12.         tbl.Columns.Add("CustomerPK", GetType(Integer))
  13.         tbl.Columns.Add("CustomerID", GetType(String))
  14.         tbl.Columns.Add("CustomerName", GetType(String))
  15.         tbl.Columns.Add("Address", GetType(String))
  16.         '//
  17.         If Conn.State = ConnectionState.Closed Then Conn.Open()
  18.         Dim Cmd = New OleDbCommand(strSQL, Conn)
  19.         Dim DR As OleDbDataReader = Cmd.ExecuteReader
  20.         While DR.Read()
  21.             If DR.HasRows Then
  22.                 tbl.Rows.Add(New Object() { _
  23.                         DR.Item("CustomerPK").ToString, _
  24.                         DR.Item("CustomerID").ToString, _
  25.                         DR.Item("CustomerName").ToString, _
  26.                         GetAddress(DR.Item("Address").ToString, DR.Item("Amphur").ToString, DR.Item("ProvinceName").ToString, DR.Item("PostCode").ToString, DR.Item("ProvinceFK").ToString)})
  27.             End If
  28.         End While
  29.         DR.Close()
  30.         Cmd.Dispose()

  31.         '/ RETURN
  32.         Return tbl
  33.     End Function
คัดลอกไปที่คลิปบอร์ด

มาดูโค้ดกันเถอะ ...
  1. ' / --------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / More: http://www.g2gnet.com/webboard
  8. ' /
  9. ' / Purpose: Unbound Data method and bring the data fields together.
  10. ' / Microsoft Visual Basic .NET (2010) + MS Access 2007+
  11. ' /
  12. ' / This is open source code under @CopyRight by Thongkorn Tubtimkrob.
  13. ' / You can not distribute this code for free. Without the consent of the developer.
  14. ' / --------------------------------------------------------------------
  15. Imports System.Data.OleDb

  16. Public Class frmUnboundDataGrid

  17.     Dim Conn As New OleDbConnection

  18.     ' / --------------------------------------------------------------------
  19.     ' / Start here.
  20.     Private Sub frmUnboundDataGrid_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  21.         Dim strPathData As String = Application.StartupPath
  22.         strPathData = MyPath(strPathData) & "data"
  23.         '// Connect DataBase.
  24.         Call ConnectDataBase(strPathData)

  25.         '// Load Data into the DataGridView1.
  26.         Me.DataGridView1.DataSource = GetDataTable()
  27.         '// Initialize DataGridView
  28.         Call SetupDataGridView(DataGridView1)
  29.     End Sub

  30.     ' / --------------------------------------------------------------------
  31.     ' / อ่านข้อมูลจากฟิลด์ที่ระบุหรือต้องการ เข้ามาเก็บไว้ยัง DataTable
  32.     Private Function GetDataTable() As DataTable
  33.         Dim strSQL As String = _
  34.             " SELECT tblCustomer.CustomerPK, tblCustomer.CustomerID, tblCustomer.CustomerName, " & _
  35.             " tblCustomer.Address, tblCustomer.Amphur, tblCustomer.ProvinceFK, tblProvince.ProvinceName, tblCustomer.PostCode " & _
  36.             " FROM tblCustomer INNER JOIN tblProvince ON tblCustomer.ProvinceFK = tblProvince.ProvincePK " & _
  37.             " ORDER BY CustomerPK "
  38.         '/
  39.         Dim tbl As New DataTable
  40.         '// เพิ่มหลักเข้าสู่ DataTable
  41.         tbl.Columns.Add("CustomerPK", GetType(Integer))
  42.         tbl.Columns.Add("CustomerID", GetType(String))
  43.         tbl.Columns.Add("CustomerName", GetType(String))
  44.         tbl.Columns.Add("Address", GetType(String))
  45.         '//
  46.         If Conn.State = ConnectionState.Closed Then Conn.Open()
  47.         Dim Cmd = New OleDbCommand(strSQL, Conn)
  48.         Dim DR As OleDbDataReader = Cmd.ExecuteReader
  49.         While DR.Read()
  50.             If DR.HasRows Then
  51.                 tbl.Rows.Add(New Object() { _
  52.                         DR.Item("CustomerPK").ToString, _
  53.                         DR.Item("CustomerID").ToString, _
  54.                         DR.Item("CustomerName").ToString, _
  55.                         GetAddress(DR.Item("Address").ToString, DR.Item("Amphur").ToString, DR.Item("ProvinceName").ToString, DR.Item("PostCode").ToString, DR.Item("ProvinceFK").ToString)})
  56.             End If
  57.         End While
  58.         DR.Close()
  59.         Cmd.Dispose()

  60.         '/ RETURN
  61.         Return tbl
  62.     End Function

  63.     ' / --------------------------------------------------------------------
  64.     '// รับค่าค่าเข้ามายังฟังค์ชั่น เพื่อจัดการแสดงผล
  65.     Private Function GetAddress(ByVal Address As String, ByVal Amphur As String, ByVal Province As String, ByVal PostCode As String, ByVal ProvinceFK As Integer) As String
  66.         GetAddress = String.Empty
  67.         '// จังหวัดที่ไม่ใช่กรุงเทพ (ProvinceFK = 3 คือ กรุงเทพมหานคร)
  68.         If Val(ProvinceFK) <> 3 Then
  69.             If Amphur <> "" And Province <> "" Then
  70.                 GetAddress = Address & " อ." & Amphur & " จ." & Province & " " & PostCode
  71.             ElseIf Amphur <> "" And Province = "" Then
  72.                 GetAddress = Address & " อ." & Amphur & " " & PostCode
  73.             ElseIf Amphur = "" And Province <> "" Then
  74.                 GetAddress = Address & " จ." & Province & " " & PostCode
  75.             Else
  76.                 GetAddress = Address
  77.             End If

  78.             '// กรุงเทพ
  79.         Else
  80.             If Amphur <> "" And Province <> "" Then
  81.                 GetAddress = Address & " เขต" & Amphur & " " & Province & " " & PostCode
  82.             ElseIf Amphur <> "" And Province = "" Then
  83.                 GetAddress = Address & " เขต" & Amphur & " " & PostCode
  84.             ElseIf Amphur = "" And Province <> "" Then
  85.                 GetAddress = Address & " " & Province & " " & PostCode
  86.             Else
  87.                 GetAddress = Address
  88.             End If
  89.         End If
  90.         '// คืนค่า
  91.         Return GetAddress
  92.     End Function

  93.     ' / --------------------------------------------------------------------
  94.     ' / Initialize DataGridView.
  95.     Private Sub SetupDataGridView(ByRef DGV As DataGridView)
  96.         With DGV
  97.             .RowHeadersVisible = True ' False
  98.             .AllowUserToAddRows = False
  99.             .AllowUserToDeleteRows = False
  100.             .AllowUserToResizeRows = False
  101.             .MultiSelect = False
  102.             .ReadOnly = True
  103.             .SelectionMode = DataGridViewSelectionMode.FullRowSelect
  104.             '/ Autosize Column
  105.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  106.             .AutoResizeColumns()
  107.             '// Even-Odd Color
  108.             .AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
  109.             '// Font data rows.
  110.             .Font = New Font("Tahoma", 9)
  111.         End With
  112.         '/ Adjust Header Styles
  113.         With DGV
  114.             With .ColumnHeadersDefaultCellStyle
  115.                 .BackColor = Color.Navy
  116.                 .ForeColor = Color.Black
  117.                 .Font = New Font("Tahoma", 9, FontStyle.Bold)
  118.             End With
  119.         End With

  120.         '// Customize the header.
  121.         With DGV
  122.             With .Columns(0)
  123.                 .Name = "CustomerPK"
  124.                 .HeaderText = "CustomerPK"
  125.                 .Visible = False
  126.             End With
  127.             With .Columns(1)
  128.                 .Name = "CustomerID"
  129.                 .HeaderText = "รหัสลูกค้า"
  130.                 .ReadOnly = True
  131.             End With
  132.             With .Columns(2)
  133.                 .Name = "CustomerName"
  134.                 .HeaderText = "ชื่อลูกค้า"
  135.             End With
  136.             With .Columns(3)
  137.                 .Name = "Address"
  138.                 .HeaderText = "ที่อยู่"
  139.             End With
  140.         End With
  141.     End Sub

  142.     ' / --------------------------------------------------------------------
  143.     ' / Connect Database.
  144.     Sub ConnectDataBase(ByVal PathData As String)
  145.         Dim strConn As String = _
  146.             "Provider = Microsoft.ACE.OLEDB.12.0; " & _
  147.             " Data Source = " & PathData & "MyDB.accdb;"
  148.         Try
  149.             Conn = New OleDbConnection(strConn)
  150.             '// Test Connection
  151.             Conn.Open()
  152.             'MsgBox("Connection Complete.")
  153.         Catch ex As Exception
  154.             MessageBox.Show(ex.Message, "ERROR")
  155.             End
  156.         End Try
  157.     End Sub

  158.     ' / --------------------------------------------------------------------
  159.     ' / Config path.
  160.     Function MyPath(ByVal AppPath As String) As String
  161.         AppPath = AppPath.ToLower
  162.         AppPath = AppPath.Replace("\bin\debug", "").Replace("\release\debug", "").Replace("\bin\x86\debug", "")
  163.         If Microsoft.VisualBasic.Right(AppPath, 1) <> "" Then AppPath = AppPath & ""
  164.         Return AppPath
  165.     End Function

  166.     Private Sub frmUnboundDataGrid_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  167.         Me.Dispose()
  168.         Application.Exit()
  169.     End Sub
  170. End Class
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-3-29 17:36 , Processed in 0.225612 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้