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

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

[VB6] การจัดกลุ่มใน SharpGrid ActiveX แบบ Bound Data Run Time

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

308

กระทู้

498

โพสต์

5973

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
5973




สำหรับ Visual Basic ทุกรุ่น วิธีการที่เขียนโค้ดคำสั่งขึ้นมา แล้วสั่งรันมันถึงจะเห็นผล เราเรียกว่า Run-Time ซึ่งจะต่างไปจากการจับลากมาวางแปะๆ ปรับโน่นนั่นนี่แสดงผลให้เห็นทันทีเป็นอันจบ อันนี้เรียกว่า Design-Time ... ในการพัฒนาโปรแกรมจริงๆ เราจะต้องอาศัยทั้ง 2 วิธีนั่นแหละ แต่ทุกๆหัวข้อแอดมินจะเน้นย้ำวิธีการแบบ Run-Time มากกว่า เพราะมันจะเกิดความยืดหยุ่นให้กับตัวโปรแกรม จะหดจะขยายได้ดั่งใจนึก จะได้ไม่เป็นภาระของลูกหลานที่มาอ่านโค้ดตามหลังเรา และที่สำคัญมันจะเป็นพื้นฐานต่อ-ยอดออกไปยังการใช้งานเทคโนโลยี Net Framework อย่างไม่ยากเย็นนัก ... สำหรับวันนี้แอดมินจะมาแสดงโค้ด
การจัดกลุ่มข้อมูลใน SharpGrid แบบผูกข้อมูล (Bound Data Control) ในลักษณะแบบ Run-Time กันน่ะครับ ...

[VB6] แจกโค้ดการใช้งานตารางกริด SharpGrid Bound Data Control ในแบบ Run Time
[VB6] แจกโค้ดการใช้งานตารางกริด SharpGrid UnBound Data Control ในแบบ Run Time

ดาวน์โหลดชุดติดตั้ง SharpGrid ได้ที่นี่ (เฉพาะสมาชิกเท่านั้น) ...


มาดูโค้ดกันเถอะ ... สำหรับฟอร์มหลัก
  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 : Sample SharpGrid ActiveX for Bound Data & Grouping Run-Time.
  10. ' / Microsoft Visual Basic 6.0 (SP6)
  11. ' /
  12. ' / This is open source code under @CopyLeft by Thongkorn/Common Tubtimkrob.
  13. ' / You can modify and/or distribute without to inform the developer.
  14. ' / -----------------------------------------------------------------------------------------------

  15. Option Explicit

  16. ' / -----------------------------------------------------------------------------------------------
  17. ' / เลือกการแสดงผลแบบกลุ่ม
  18. Private Sub cmbGroup_Click()
  19.     If cmbGroup.ListIndex = 0 Then
  20.         cmdCollapse.Enabled = False
  21.         cmdExpand.Enabled = False
  22.     Else
  23.         cmdCollapse.Enabled = True
  24.         cmdExpand.Enabled = True
  25.     End If
  26.     ' / Bound Data เข้าสู่ SharpGrid
  27.     Call SGGridCountryBound
  28. End Sub

  29. Private Sub cmdCollapse_Click()
  30.     SGGridData.CollapseAll
  31. End Sub

  32. Private Sub cmdExpand_Click()
  33.     SGGridData.ExpandAll
  34. End Sub

  35. ' / -----------------------------------------------------------------------------------------------
  36. Private Sub Form_Load()
  37. ' / -----------------------------------------------------------------------------------------------
  38. On Error GoTo ErrorHandler
  39.     Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
  40.     ' / เชื่อมต่อกับไฟล์ฐานข้อมูล
  41.     Call OpenDataBase
  42.    
  43.     ' / ใส่รายการกลุ่มใน ComboBox
  44.     cmbGroup.AddItem "แสดงผลทั้งหมด"    ' / Index = 0
  45.     cmbGroup.AddItem "จัดกลุ่มตามโซน"       ' / Index = 1
  46.     cmbGroup.ListIndex = 0  ' / เริ่มต้นที่แสดงผลทั้งหมด
  47.    
  48.     ' / ตั้งค่าเริ่มต้นให้กับ SharpGrid
  49.     Call SetupSGGridCountry
  50.     ' / Bound Data เข้าสู่ SharpGrid
  51.     Call SGGridCountryBound
  52.    
  53. ExitProc:
  54.     Exit Sub
  55.    
  56. ErrorHandler:
  57.     MsgBox "Error : " & Err.Number & vbCrLf & Err.Description, vbOKOnly + vbExclamation, "รายงานความผิดพลาด"
  58.     Resume ExitProc

  59. End Sub

  60. ' / -----------------------------------------------------------------------------------------------
  61. Sub SetupSGGridCountry()
  62. ' / -----------------------------------------------------------------------------------------------
  63.     ' / Initialize grid properties
  64.     ' / การตั้งค่าคุณสมบัติ (Properties) ในแบบ Run Time (สั่งรันโปรแกรมจึงจะเห็นผล)
  65.     ' / เป็นการตั้งค่ามาตรฐานที่เราต้องใช้งานกันเป็นประจำ แบบไม่ต้องมาท่องจำ
  66.     With SGGridData
  67.         ' ============ IMPORTANT ===========
  68.         ' / สำหรับการจัดกลุ่มแบบ Drag & Drop
  69.         .GroupByBoxVisible = False  ' <----- ปิดการ Drag & Drop ของ Group
  70.         .CacheAllRecords = True
  71.         '.GroupByBoxText = "Drag a column header here to group by that column"
  72.         .GroupByBoxText = "ลากส่วนหัวของแต่ละหลักมาวางเพื่อทำการจัดกลุ่ม"
  73.         ' ===================================
  74.         .FitLastColumn = True
  75.         .Appearance = sg3D
  76.         .SpecialMode = sgModeListBox
  77.         .CellsBorderVisible = True
  78.         .AutoResize = sgAutoResizeColumns   ' / ปรับขนาดหลักแบบออโต้
  79.         .GroupIndentation = 225 ' / ระยะการจัดกลุ่มเยื้องมาจากทางซ้าย
  80.         
  81.         .DefaultRowHeight = 390
  82.         .RowHeightMin = 390
  83.         .HeadingColCount = 1
  84.         
  85.         .HeadingGridLinesColor = vbBlack
  86.         .HeadingGridLines = sgGridLineFlat
  87.         
  88.         .EvenOddStyle = sgEvenOddRows
  89.         .ColorOdd = &HEFEFE0
  90.         
  91.         .CellTips = sgCellTipsFloat
  92.         .CellTipsDelay = 400
  93.         .ScrollBarTips = sgScrollTipsVertical
  94.         
  95.         ' / การแสดงผลปกติในตารางกริด
  96.         With .Styles("Normal")
  97.             .BkgStyle = sgCellBkgSolid
  98.             .Font.Name = "Tahoma"
  99.             .Font.Size = 8
  100.             .Padding = 18
  101.         End With
  102.         ' / ส่วนหัวของหลัก (Columns)
  103.         With .Styles("Heading")
  104.             .BackColor = RGB(205, 0, 0)
  105.             .ForeColor = vbWhite
  106.             .Font.Name = "Tahoma"
  107.             .Font.Size = 9
  108.             .Font.Bold = True
  109.             .Padding = 75
  110.         End With
  111.         ' / ส่วนหัวของ Group
  112.         With .Styles("GroupHeader")
  113.             .Font.Size = 9
  114.             .Font.Bold = True
  115.             .BackColor = RGB(241, 239, 226)
  116.             .BkgStyle = sgCellBkgSolid
  117.             .Padding = 30
  118.             .BorderColor = RGB(241, 207, 0)
  119.             .Borders = sgCellBorderBottom
  120.             .BorderSize = 1
  121.         End With
  122.         ' / ส่วนด้านล่างหรือสรุปของ Group
  123.         With .Styles("GroupFooter")
  124.             .Font.Size = 9
  125.             .Font.Name = "Tahoma"
  126.             .ForeColor = vbBlue
  127.             .BackColor = RGB(255, 255, 224)
  128.             .BkgStyle = sgCellBkgSolid
  129.             .Padding = 75
  130.             .BorderColor = RGB(255, 207, 0)
  131.             .Borders = sgCellBorderBottom
  132.             .BorderSize = 50
  133.             .TextAlignment = sgAlignLeftCenter
  134.         End With
  135.         
  136.         With .Styles("Tip")
  137.             .Font.Size = 10
  138.             .Padding = 40
  139.         End With
  140.         ' / แถวที่เราเลือกหรือโฟกัส
  141.         With .Styles("Selection")
  142.             .BackColor = RGB(0, 170, 0)
  143.             .ForeColor = vbWhite
  144.             .BkgStyle = sgCellBkgSolid
  145.         End With
  146.         ' / แถวที่เราไม่ได้โฟกัส
  147.         With .Styles("InactiveSelection")
  148.             .BackColor = RGB(0, 170, 0)
  149.             .ForeColor = vbWhite
  150.             .BkgStyle = sgCellBkgSolid
  151.         End With
  152.    
  153.     End With
  154.    
  155. End Sub
  156. ' / -----------------------------------------------------------------------------------------------
  157. Sub SGGridCountryBound()
  158. ' / -----------------------------------------------------------------------------------------------
  159.    
  160.     ' สร้าง Instance ขึ้นมาใหม่ พร้อมกับตัดการเชื่อมต่อเดิมทิ้ง (หากลืม)
  161.     Set RS = New ADODB.Recordset
  162.     ' / ****************** I M P O R T A N T ******************
  163.     ' / Create select statement, sample join 2 tables.
  164.     ' / การ Bound Data เมื่อเวลาแสดงผลในแต่ละหลัก (Column) มันจะถูกจัดเรียงตามการชื่อฟิลด์ใน Query
  165.     ' / ย้ำอีกทีว่าหลักจะถูกจัดเรียงตามชื่อฟิลด์
  166.     Statement = " SELECT Countries.CountryPK, Countries.Flag, Countries.A2, Countries.Country, Countries.Capital, " & _
  167.                         " Countries.Population, Zones.ZoneName " & _
  168.                         " FROM Countries INNER JOIN Zones ON Countries.ZoneFK = Zones.ZonePK "

  169.     ' ////////// I M P O R T A N T /////////////
  170.     ' / การจัดเรียงข้อมูล (ORDER) จะมีผลต่อการแสดงผลในการจัดกลุ่ม
  171.     Select Case cmbGroup.ListIndex
  172.         Case 0:
  173.             Statement = Statement & " ORDER BY Countries.A2 "
  174.         Case 1:
  175.             ' / จัดกลุ่มตามโซน ก็จะต้องให้เกิดการจัดเรียงข้อมูลตาม ZoneName
  176.             Statement = Statement & " ORDER BY Zones.ZoneName, Countries.A2 "
  177.     End Select

  178.     ' ============ IMPORTANT ==========
  179.     SGGridData.DataMode = sgBound
  180.     ' ==================================
  181.     RS.CursorLocation = adUseClient
  182.     RS.Open Statement, ConnDB, adOpenForwardOnly, adLockReadOnly, adCmdText
  183.     ' ======================= ผูกเข้ากับตาราง RecordSet =========================
  184.     Set SGGridData.DataSource = RS
  185.     ' =====================================================================
  186.     'SGGridData.DataRowCount = RS.RecordCount
  187.    
  188.     With SGGridData
  189.         ' / หลัก 0 นี่คือหลักที่แสดงหมายเลขแถวครับ
  190.         .Rows.At(0).Height = 420
  191.         ' / คลิ๊กส่วนหัวให้สามารถจัดเรียงข้อมูลแบบน้อยไปหามาก และคลิ๊กอีกทีจะเรียงจากมากไปหาน้อย
  192.         .ColumnClickSort = True
  193.         ' / หลัก 0 ที่แสดงหมายเลขแถวจะถูกซ่อน ไม่ให้แสดงผล
  194.         .Columns(0).Hidden = True
  195.         ' / ไม่แสดงผลลำดับที่
  196.         .RowNumbering = False
  197.         ' /
  198.         .Columns(1).Caption = "CountryPK"
  199.         ' / ซ่อนหลัก Primary Key (หลัก 1 เพราะหลัก 0 คือหลักแสดงผลลำดับแถว)
  200.         .Columns(1).Hidden = True
  201.         '
  202.         .Columns(2).Caption = "ธงชาติ"
  203.         .Columns(2).Width = 800
  204.         .Columns(3).Caption = "ชื่อย่อ"
  205.         .Columns(3).Width = 800
  206.         .Columns(4).Caption = "ชื่อประเทศ"
  207.         .Columns(4).Width = 2000
  208.         .Columns(5).Caption = "ชื่อเมืองหลวง"
  209.         .Columns(5).Width = 1800
  210.         .Columns(6).Caption = "จำนวนประชากร"
  211.         .Columns(6).Width = 1600
  212.         .Columns(6).Style.TextAlignment = sgAlignRightCenter
  213.         .Columns(6).DataType = sgtCurrency
  214.         .Columns(6).Style.Format = "#,##0.00"
  215.         .Columns(7).Caption = "โซน"
  216.         .Columns(7).Hidden = False
  217.     End With
  218.     ' ========== Start of Group ==========
  219.     If cmbGroup.ListIndex = 1 Then
  220.         Dim Grp1 As SGGroup
  221.         With SGGridData
  222.             ' Add group
  223.             ' / ZoneName คือต้องการจัดกลุ่มตามชื่อโซน
  224.             Set Grp1 = .Groups.Add("ZoneName", sgNoSorting, False, True)
  225.             ' / ในส่วนการแสดงผล
  226.             Grp1.HeaderCaption = "กลุ่มโซน"
  227.             Grp1.FetchHeaderStyle = True
  228.             Grp1.HeaderTextSource = sgGrpHdrCaptionAndValue
  229.             ' / หาค่า Summary ของหลัก Population (การคำนวณอื่นๆ ขอให้ไปดูที่ Help น่ะครับ)
  230.             Grp1.Calculations.Add sgCalcSum, "Population"
  231.             ' / แสดงผลการรวมจำนวนประชากรที่กลุ่มของ Footer (สรุปผล)
  232.             Grp1.FooterTextSource = sgGrpFooterFormula
  233.             Grp1.FooterFormula = "'Total populations:    ' & Format((GroupCalc(1)),'#,##0.00')  & ' คน." & "'"
  234.             
  235.             ' / ปิดการแสดงผลในหลักที่ 7 เพราะเรานำหลักนี้ไปจัดกลุ่ม
  236.             .Columns(7).Hidden = True
  237.         End With
  238.         '
  239.         Set Grp1 = Nothing
  240.         ' / หากต้องการแสดงผลการจัดกลุ่มแบบมีกิ่งก้านสาขา ให้สั่ง ExpandAll
  241.         SGGridData.ExpandAll
  242.     End If
  243.     ' ========== End of Group ==========
  244.    
  245.     ' / ต้องตัดการเชื่อมต่อ RecordSet ทุกครั้ง
  246.     RS.Close:   Set RS = Nothing
  247. End Sub

  248. ' / -----------------------------------------------------------------------------------------------
  249. ' / แสดง Primary Key ที่ถูกซ่อนเอาไว้ เพื่อนำไปใช้งาน
  250. Private Sub SGGridData_DblClick()
  251. ' / -----------------------------------------------------------------------------------------------
  252.     ' / หลัก 1 คือ Primary Key ที่เรานำไปซ่อนไว้ไม่ให้ Users มองเห็น
  253.     SGGridData.Col = 1
  254.     ' / หากดับเบิ้ลคลิ๊กเมาส์แล้ว เจอแถวที่แสดงผลแบบกลุ่ม ก็ให้ออกไปจากโปรแกรมย่อย
  255.     If Not IsNumeric(SGGridData.Text) Then Exit Sub
  256.    
  257.     MsgBox "CountryPK = " & Val(SGGridData.Text)
  258. End Sub

  259. ' / -----------------------------------------------------------------------------------------------
  260. ' / เหตุการณ์ที่เกิดก่อนการเปลี่ยนแปลงในการจัดกลุ่มในแต่ละหลัก (Column)
  261. ' / ไม่ได้ใช้สำหรับการจัดกลุ่มในโค้ดชุดนี้
  262. Private Sub SGGridData_BeforeGroupChange( _
  263.     ByVal Operation As DDSharpGridOLEDB2U.sgGroupOperation, _
  264.     ByVal GroupOrColIndex As Long, _
  265.     ByVal NewIndex As Long, _
  266.     SortOrder As DDSharpGridOLEDB2U.sgSortOrder, _
  267.     SortType As DDSharpGridOLEDB2U.sgSortType, _
  268.     ShowFooter As Boolean, _
  269.     Cancel As Boolean _
  270.     )
  271. ' / -----------------------------------------------------------------------------------------------
  272.     If Operation = sgGroupRemove Then
  273.         SGGridData.Columns(SGGridData.Groups(GroupOrColIndex).GroupingColumn).Hidden = False
  274.     ElseIf Operation = sgGroupAdd Then
  275.         SGGridData.Columns(GroupOrColIndex).Hidden = True
  276.     End If
  277. End Sub

  278. Private Sub Form_Resize()
  279.     On Error Resume Next
  280.     If Me.ScaleWidth < 120 Or Me.ScaleHeight < 120 Then Exit Sub
  281.     fraData.Height = Me.ScaleHeight - 30
  282.     fraData.Move 15, 0, Me.ScaleWidth - 15
  283.     SGGridData.Move 15, 795, fraData.Width - (SGGridData.Left) - 15, fraData.Height - 300
  284. End Sub

  285. Private Sub Form_Unload(Cancel As Integer)
  286. On Error Resume Next
  287.     If Dir$(App.Path & "\*.tmp") <> "" Then
  288.         Kill App.Path & "\*.tmp"
  289.     End If
  290.     '
  291.     Call CloseDataBase
  292.     End
  293. End Sub
คัดลอกไปที่คลิปบอร์ด


มาดูโค้ดในส่วนของโมดูลหากิน ...
  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 only)
  6. ' / Facebook: https://www.facebook.com/commonindy (For International)
  7. ' / Purpose : Standard module for connect database and declare global variable.
  8. ' / Microsoft Visual Basic 6.0 Service Pack 6
  9. ' / -----------------------------------------------------------------------------------------------
  10. Option Explicit

  11. Global ConnDB As New ADODB.Connection
  12. Global RS As New ADODB.Recordset    ' / RecordSet หลัก
  13. Global DS As New ADODB.Recordset    ' / RecordSet สำรอง
  14. 'Global RstData As New ADODB.Recordset   ' / ใช้ในการพิมพ์รายงาน
  15. Global Statement As String
  16. 'Global SQLStmt As String

  17. ' / -----------------------------------------------------------------------------------------------
  18. Public Sub OpenDataBase()
  19. On Error GoTo Err_Handler
  20. Dim DB_File As String
  21. Dim CnStr As String
  22.     DB_File = App.Path
  23.     If Right$(DB_File, 1) <> "" Then DB_File = DB_File & ""
  24.     DB_File = DB_File & "Countries.MDB"
  25.     ' Open a connection.
  26.     Set ConnDB = New ADODB.Connection
  27.     ConnDB.ConnectionString = _
  28.         "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  29.         "Data Source=" & DB_File & ";" & _
  30.         "Persist Security Info=False"
  31.         '" Jet OLEDB:Database Password=" & "password" & ";" '& _
  32.         '" Engine Type=5;"
  33.     ConnDB.Open
  34.     Exit Sub
  35. Err_Handler:
  36.     MsgBox "Open Database Error : " & vbCrLf & Err.Number & " " & Err.Description
  37.     End
  38. End Sub

  39. Public Sub CloseDataBase()
  40.     ' ตรวจสอบว่ามีการเชื่อมโยง - Connect ข้อมูลหรือไม่
  41.     If ConnDB.State = adStateOpen Then
  42.         ConnDB.Close
  43.         Set ConnDB = Nothing
  44.     End If
  45. End Sub

  46. ' / -----------------------------------------------------------------------------------------------
  47. ' / ฟังค์ชั่นแก้ไขในการ SendKeys ซึ่งใน Windows 8+ 64 บิท จะมีปัญหา
  48. Public Sub Sendkeys(Text As String, Optional Wait As Boolean = False)
  49.     Dim WshShell As Object
  50.     Set WshShell = CreateObject("Wscript.shell")
  51.     WshShell.Sendkeys Text, Wait
  52.     Set WshShell = Nothing
  53. End Sub
คัดลอกไปที่คลิปบอร์ด


ดาวน์โหลดโค้ดต้นฉบับ VB6 ได้ที่นี่ ...




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

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

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

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

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

GMT+7, 2024-3-29 13:45 , Processed in 0.197624 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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