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

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

[VB.NET] การค้นหาข้อมูลหรือคอนเทนท์จาก JSON Format แสดงผลในตารางกริด

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

308

กระทู้

498

โพสต์

5973

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
5973




Add References ...

การค้นหาข้อมูลหรือคอนเทนท์จาก JSON Format (JavaScript Object Notation) มาแสดงผลลงในตารางกริด ซึ่งวิธีการเราไม่ได้ค้นหาคอนเทนต์ภายใน JSON โดยตรง แต่เรา Deserialize JSON แยกเอาข้อมูลมาเก็บไว้ใน DataTable ก่อน จากนั้นก็จะทำ BindingSource อีกที ในการค้นหาก็จะ Method Filter ...

Thailand Geography JSON ... ทางผู้ที่ให้ข้อมูลมาบอกว่าเป็นรายชื่อจังหวัด อำเภอ ตำบล และรหัสไปรษณีย์ ที่อัพเดตจากกรมการปกครอง ปี พ.ศ.2565

มาดูโค้ดกันเถอะ ...
  1. '// Special Thank ... Original JSON data.
  2. '// https://github.com/thailand-geography-data/thailand-geography-json

  3. Imports Newtonsoft.Json
  4. Imports System.IO

  5. Public Class frmFilterJson

  6.     Private JsonSource As New BindingSource()

  7.     Private Sub frmFilterJson_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  8.         Dim json = File.ReadAllText(MyPath(Application.StartupPath) & "src\geography.json")
  9.         Dim dt = JsonConvert.DeserializeObject(Of DataTable)(json)
  10.         dt.AcceptChanges()
  11.         '// Binding Source.
  12.         JsonSource.DataSource = dt
  13.         dgvData.DataSource = JsonSource
  14.         With dgvData.Columns
  15.             .Remove("provinceCode")
  16.             .Remove("districtCode")
  17.             .Remove("subdistrictCode")
  18.         End With
  19.         Call SetupGridView()
  20.         Me.lblRecordCount.Text = "Total: " & Format(dgvData.RowCount, "#,##") & " Records."
  21.         Me.Size = New Size(1180, 800)
  22.         Me.CenterToScreen()
  23.     End Sub

  24.     '// Filter.
  25.     Private Sub txtFilterJson_TextChanged(sender As Object, e As System.EventArgs) Handles txtFilterJson.TextChanged
  26.         If rdoFilterAll.Checked Then
  27.             JsonSource.Filter = _
  28.                 " provinceNameEn LIKE " & "'%" & txtFilterJson.Text & "%'" & _
  29.                 " OR provinceNameTh LIKE " & "'%" & txtFilterJson.Text & "%'" & _
  30.                 " OR districtNameEn LIKE " & "'%" & txtFilterJson.Text & "%'" & _
  31.                 " OR districtNameTh LIKE " & "'%" & txtFilterJson.Text & "%'" & _
  32.                 " OR subdistrictNameEn LIKE " & "'%" & txtFilterJson.Text & "%'" & _
  33.                 " OR subdistrictNameTh LIKE " & "'%" & txtFilterJson.Text & "%'"
  34.         ElseIf rdoProvinceNameEn.Checked Then
  35.             JsonSource.Filter = "provinceNameEn LIKE " & "'%" & txtFilterJson.Text & "%'"
  36.         ElseIf rdoProvinceNameTh.Checked Then
  37.             JsonSource.Filter = "provinceNameTh LIKE " & "'%" & txtFilterJson.Text & "%'"
  38.         ElseIf rdoDistrictNameEn.Checked Then
  39.             JsonSource.Filter = "districtNameEn LIKE " & "'%" & txtFilterJson.Text & "%'"
  40.         ElseIf rdoDistrictNameTh.Checked Then
  41.             JsonSource.Filter = "districtNameTh LIKE " & "'%" & txtFilterJson.Text & "%'"
  42.         End If
  43.         '//
  44.         Call SetupGridView()
  45.         Me.lblRecordCount.Text = "Total: " & Format(dgvData.RowCount, "#,##") & " Records."
  46.     End Sub

  47.     '// Initialized DataGridView.
  48.     Private Sub SetupGridView()
  49.         With dgvData
  50.             .RowHeadersVisible = True
  51.             .AllowUserToAddRows = False
  52.             .AllowUserToDeleteRows = False
  53.             .AllowUserToResizeRows = False
  54.             .MultiSelect = False
  55.             .SelectionMode = DataGridViewSelectionMode.FullRowSelect
  56.             .ReadOnly = True
  57.             '// Data rows
  58.             .Font = New Font("Tahoma", 11)
  59.             .RowTemplate.MinimumHeight = 27
  60.             .RowTemplate.Height = 27
  61.             '// Column Header
  62.             .ColumnHeadersHeight = 30
  63.             .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
  64.             '// Autosize Column
  65.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  66.             '// Header
  67.             With .ColumnHeadersDefaultCellStyle
  68.                 .BackColor = Color.RoyalBlue
  69.                 .ForeColor = Color.White
  70.                 .Font = New Font(dgvData.Font, FontStyle.Bold)
  71.             End With
  72.             .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
  73.             .ColumnHeadersHeight = 36
  74.             '/ Accept changes to the header's background color.
  75.             .EnableHeadersVisualStyles = False
  76.             '// Even-Odd Color of Rows.
  77.             .AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
  78.             '// Even-Odd Color of Columns.
  79.             For iCol As Integer = 0 To dgvData.Columns.Count - 1
  80.                 '// If any integer Mod by 2 and gets the answer is 0 so even number, 1 is an odd number.
  81.                 If iCol Mod 2 = 1 Then
  82.                     dgvData.Columns(iCol).HeaderCell.Style.BackColor = Color.BlueViolet
  83.                 Else
  84.                     dgvData.Columns(iCol).HeaderCell.Style.BackColor = Color.SeaGreen
  85.                 End If
  86.             Next
  87.         End With
  88.     End Sub

  89.     Private Sub rdoProvince_Click(sender As Object, e As System.EventArgs) Handles rdoFilterAll.Click, rdoProvinceNameEn.Click, rdoProvinceNameTh.Click, rdoDistrictNameEn.Click, rdoDistrictNameTh.Click
  90.         txtFilterJson.Focus()
  91.     End Sub

  92.     Private Sub frmFilterJson_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  93.         Me.Dispose()
  94.         GC.SuppressFinalize(Me)
  95.         Application.Exit()
  96.     End Sub

  97.     ' / --------------------------------------------------------------------------------
  98.     ' / Get my project path
  99.     ' / AppPath = C:\My Project\bin\debug
  100.     ' / Replace "\bin\debug" with ""
  101.     ' / Return : C:\My Project\
  102.     Function MyPath(ByVal AppPath As String) As String
  103.         '/ Return Value
  104.         MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  105.         '// If not found folder then put the \ (BackSlash) at the end.
  106.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  107.     End Function
  108. End Class
คัดลอกไปที่คลิปบอร์ด

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


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

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

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

5

โพสต์

132

เครดิต

Member

Rank: 2

เครดิต
132
โพสต์ 2023-8-25 10:40:43 | ดูโพสต์ทั้งหมด

ขอบคุณครับ

1

กระทู้

12

โพสต์

155

เครดิต

Member

Rank: 2

เครดิต
155
โพสต์ 2023-9-5 14:27:33 | ดูโพสต์ทั้งหมด

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

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

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

GMT+7, 2024-3-29 19:34 , Processed in 0.601921 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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