|


Add References ...
การค้นหาข้อมูลหรือคอนเทนท์จาก JSON Format (JavaScript Object Notation) มาแสดงผลลงในตารางกริด ซึ่งวิธีการเราไม่ได้ค้นหาคอนเทนต์ภายใน JSON โดยตรง แต่เรา Deserialize JSON แยกเอาข้อมูลมาเก็บไว้ใน DataTable ก่อน จากนั้นก็จะทำ BindingSource อีกที ในการค้นหาก็จะ Method Filter ...
Thailand Geography JSON ... ทางผู้ที่ให้ข้อมูลมาบอกว่าเป็นรายชื่อจังหวัด อำเภอ ตำบล และรหัสไปรษณีย์ ที่อัพเดตจากกรมการปกครอง ปี พ.ศ.2565
มาดูโค้ดกันเถอะ ...
- '// Special Thank ... Original JSON data.
- '// https://github.com/thailand-geography-data/thailand-geography-json
- Imports Newtonsoft.Json
- Imports System.IO
- Public Class frmFilterJson
- Private JsonSource As New BindingSource()
- Private Sub frmFilterJson_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
- Dim json = File.ReadAllText(MyPath(Application.StartupPath) & "src\geography.json")
- Dim dt = JsonConvert.DeserializeObject(Of DataTable)(json)
- dt.AcceptChanges()
- '// Binding Source.
- JsonSource.DataSource = dt
- dgvData.DataSource = JsonSource
- With dgvData.Columns
- .Remove("provinceCode")
- .Remove("districtCode")
- .Remove("subdistrictCode")
- End With
- Call SetupGridView()
- Me.lblRecordCount.Text = "Total: " & Format(dgvData.RowCount, "#,##") & " Records."
- Me.Size = New Size(1180, 800)
- Me.CenterToScreen()
- End Sub
- '// Filter.
- Private Sub txtFilterJson_TextChanged(sender As Object, e As System.EventArgs) Handles txtFilterJson.TextChanged
- If rdoFilterAll.Checked Then
- JsonSource.Filter = _
- " provinceNameEn LIKE " & "'%" & txtFilterJson.Text & "%'" & _
- " OR provinceNameTh LIKE " & "'%" & txtFilterJson.Text & "%'" & _
- " OR districtNameEn LIKE " & "'%" & txtFilterJson.Text & "%'" & _
- " OR districtNameTh LIKE " & "'%" & txtFilterJson.Text & "%'" & _
- " OR subdistrictNameEn LIKE " & "'%" & txtFilterJson.Text & "%'" & _
- " OR subdistrictNameTh LIKE " & "'%" & txtFilterJson.Text & "%'"
- ElseIf rdoProvinceNameEn.Checked Then
- JsonSource.Filter = "provinceNameEn LIKE " & "'%" & txtFilterJson.Text & "%'"
- ElseIf rdoProvinceNameTh.Checked Then
- JsonSource.Filter = "provinceNameTh LIKE " & "'%" & txtFilterJson.Text & "%'"
- ElseIf rdoDistrictNameEn.Checked Then
- JsonSource.Filter = "districtNameEn LIKE " & "'%" & txtFilterJson.Text & "%'"
- ElseIf rdoDistrictNameTh.Checked Then
- JsonSource.Filter = "districtNameTh LIKE " & "'%" & txtFilterJson.Text & "%'"
- End If
- '//
- Call SetupGridView()
- Me.lblRecordCount.Text = "Total: " & Format(dgvData.RowCount, "#,##") & " Records."
- End Sub
- '// Initialized DataGridView.
- Private Sub SetupGridView()
- With dgvData
- .RowHeadersVisible = True
- .AllowUserToAddRows = False
- .AllowUserToDeleteRows = False
- .AllowUserToResizeRows = False
- .MultiSelect = False
- .SelectionMode = DataGridViewSelectionMode.FullRowSelect
- .ReadOnly = True
- '// Data rows
- .Font = New Font("Tahoma", 11)
- .RowTemplate.MinimumHeight = 27
- .RowTemplate.Height = 27
- '// Column Header
- .ColumnHeadersHeight = 30
- .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
- '// Autosize Column
- .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
- '// Header
- With .ColumnHeadersDefaultCellStyle
- .BackColor = Color.RoyalBlue
- .ForeColor = Color.White
- .Font = New Font(dgvData.Font, FontStyle.Bold)
- End With
- .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
- .ColumnHeadersHeight = 36
- '/ Accept changes to the header's background color.
- .EnableHeadersVisualStyles = False
- '// Even-Odd Color of Rows.
- .AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
- '// Even-Odd Color of Columns.
- For iCol As Integer = 0 To dgvData.Columns.Count - 1
- '// If any integer Mod by 2 and gets the answer is 0 so even number, 1 is an odd number.
- If iCol Mod 2 = 1 Then
- dgvData.Columns(iCol).HeaderCell.Style.BackColor = Color.BlueViolet
- Else
- dgvData.Columns(iCol).HeaderCell.Style.BackColor = Color.SeaGreen
- End If
- Next
- End With
- End Sub
- Private Sub rdoProvince_Click(sender As Object, e As System.EventArgs) Handles rdoFilterAll.Click, rdoProvinceNameEn.Click, rdoProvinceNameTh.Click, rdoDistrictNameEn.Click, rdoDistrictNameTh.Click
- txtFilterJson.Focus()
- End Sub
- Private Sub frmFilterJson_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
- Me.Dispose()
- GC.SuppressFinalize(Me)
- Application.Exit()
- End Sub
- ' / --------------------------------------------------------------------------------
- ' / Get my project path
- ' / AppPath = C:\My Project\bin\debug
- ' / Replace "\bin\debug" with ""
- ' / Return : C:\My Project\
- Function MyPath(ByVal AppPath As String) As String
- '/ Return Value
- MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
- '// If not found folder then put the \ (BackSlash) at the end.
- If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
- End Function
- End Class
คัดลอกไปที่คลิปบอร์ด
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้จากที่นี่ ...
|
ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง
คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน
x
|