-
Notifications
You must be signed in to change notification settings - Fork 0
/
category.vb
143 lines (127 loc) · 5.16 KB
/
category.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Imports System.Data.Odbc
Public Class category
Dim selectedCategoryID, level As String
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Sub showData()
dbconnection()
da = New OdbcDataAdapter("SELECT * FROM category", conn)
ds = New DataSet
da.Fill(ds, "category")
CategoryDataGridView.DataSource = ds.Tables("category")
conn.Close()
End Sub
Sub DGVButton()
DGVDeleteColumnButton.Name = "DGVDeleteColumnButton"
DGVDeleteColumnButton.HeaderText = ""
DGVDeleteColumnButton.FlatStyle = FlatStyle.Popup
DGVDeleteColumnButton.DefaultCellStyle.ForeColor = Color.Red
DGVDeleteColumnButton.Text = "Delete"
DGVDeleteColumnButton.Width = 50
DGVDeleteColumnButton.UseColumnTextForButtonValue = True
CategoryDataGridView.Columns.Add(DGVDeleteColumnButton)
DGVEditColumnButton.Name = "DGVEditColumnButton"
DGVEditColumnButton.HeaderText = ""
DGVEditColumnButton.FlatStyle = FlatStyle.Popup
DGVEditColumnButton.DefaultCellStyle.ForeColor = Color.DarkGreen
DGVEditColumnButton.Text = "Edit"
DGVEditColumnButton.Width = 50
DGVEditColumnButton.UseColumnTextForButtonValue = True
CategoryDataGridView.Columns.Add(DGVEditColumnButton)
End Sub
Sub SetDGVHeader()
CategoryDataGridView.Columns(0).HeaderText = "ID"
CategoryDataGridView.Columns(1).HeaderText = "Category Name"
End Sub
Sub DeleteRecord(param As String)
If MessageBox.Show("Are you sure want to delete category with id '" & param & "'?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
dbconnection()
command = "DELETE FROM category WHERE id='" & param & "'"
query = New OdbcCommand(command, conn)
query.ExecuteNonQuery()
If MessageBox.Show("Category Deleted", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information) = DialogResult.OK Then
showData()
End If
End If
conn.Close()
End Sub
Sub UpdateRecord(param As String)
categorydetail.Show()
categorydetail.Text = "Edit Category Information"
categorydetail.SaveButton.Text = "Update"
categorydetail.CategoryIDTextBox.Enabled = False
'Load category old data
dbconnection()
command = "SELECT * FROM category WHERE id='" & param & "'"
query = New OdbcCommand(command, conn)
reader = query.ExecuteReader()
While reader.Read()
If reader.HasRows Then
categorydetail.CategoryIDTextBox.Text = reader.Item("id")
categorydetail.CategoryNameTextBox.Text = reader.Item("name")
End If
End While
conn.Close()
End Sub
Private Sub SearchTextBox_TextChanged(sender As Object, e As EventArgs) Handles SearchTextBox.TextChanged
If SearchTextBox.Text = "" Then
Timer.Enabled = True
Else
Timer.Enabled = False
End If
dbconnection()
command = "SELECT * FROM category WHERE id LIKE '%' '" & SearchTextBox.Text & "' '%' OR name LIKE '%' '" & SearchTextBox.Text & "' '%'"
da = New OdbcDataAdapter(command, conn)
ds = New DataSet
da.Fill(ds, "category")
CategoryDataGridView.DataSource = ds.Tables("category")
If CategoryDataGridView.RowCount > 0 Then
Dim row As Integer
With CategoryDataGridView
row = .CurrentRow.Index
selectedCategoryID = .Item(0, row).Value
End With
End If
conn.Close()
End Sub
Private Sub admin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
showData()
DGVButton()
SetDGVHeader()
End Sub
Private Sub BackButton_Click(sender As Object, e As EventArgs) Handles BackButton.Click
main.Show()
Me.Dispose()
End Sub
Private Sub admin_Closed(sender As Object, e As EventArgs) Handles Me.Closed
Me.Dispose()
main.Show()
End Sub
Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
CategoryDataGridView.Columns.Clear()
showData()
SetDGVHeader()
DGVButton()
End Sub
Private Sub AddCategoryButton_Click(sender As Object, e As EventArgs) Handles AddCategoryButton.Click
categorydetail.Show()
Me.Hide()
End Sub
Private Sub CategoryDataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles CategoryDataGridView.CellClick
If CategoryDataGridView.RowCount > 0 Then
Dim row As Integer
With CategoryDataGridView
row = .CurrentRow.Index
selectedCategoryID = .Item(0, row).Value
If e.ColumnIndex = 2 Then
DeleteRecord(selectedCategoryID)
ElseIf e.ColumnIndex = 3 Then
UpdateRecord(selectedCategoryID)
End If
End With
End If
End Sub
End Class