-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
264 lines (230 loc) · 9.04 KB
/
Form1.cs
File metadata and controls
264 lines (230 loc) · 9.04 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace PV_11_KasirComboBox
{
public partial class Form1 : Form
{
private SqlCommand cmd;
private DataSet ds;
private SqlDataAdapter da;
Koneksi Konn = new Koneksi();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TampilBarang();
Bersihkan();
MunculSatuan();
}
void TampilBarang()
{
SqlConnection conn = Konn.GetConn();
try
{
conn.Open();
cmd = new SqlCommand("SELECT * FROM TBL_BARANG", conn);
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds, "TBL_BARANG");
dataGridView.DataSource = ds;
dataGridView.DataMember = "TBL_BARANG";
dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
catch (Exception G)
{
MessageBox.Show(G.ToString());
}
finally
{
conn.Close();
}
}
void CariBarang()
{
SqlConnection conn = Konn.GetConn();
try
{
conn.Open();
cmd = new SqlCommand("SELECT * FROM TBL_BARANG WHERE KodeBarang LIKE '%" + textBoxCari.Text + "%' OR NamaBarang LIKE '%" + textBoxCari.Text + "%'", conn);
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds, "TBL_BARANG");
dataGridView.DataSource = ds;
dataGridView.DataMember = "TBL_BARANG";
dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
catch (Exception G)
{
MessageBox.Show(G.ToString());
}
finally
{
conn.Close();
}
}
void Bersihkan()
{
textBoxKodeBarang.Text = "";
textBoxNamaBarang.Text = "";
textBoxHargaBeli.Text = "0";
textBoxHargaJual.Text = "0";
textBoxJumlah.Text = "0";
comboBoxSatuan.Text = "";
textBoxCari.Text = "";
TampilBarang();
}
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
textBoxKodeBarang.Text = row.Cells["KodeBarang"].Value.ToString();
textBoxNamaBarang.Text = row.Cells["NamaBarang"].Value.ToString();
textBoxHargaJual.Text = row.Cells["HargaJual"].Value.ToString();
textBoxHargaBeli.Text = row.Cells["HargaBeli"].Value.ToString();
textBoxJumlah.Text = row.Cells["JumlahBarang"].Value.ToString();
comboBoxSatuan.Text = row.Cells["SatuanBarang"].Value.ToString();
}
catch (Exception e1)
{
MessageBox.Show(e1.ToString());
}
}
void MunculSatuan()
{
comboBoxSatuan.Items.Add("Unit");
comboBoxSatuan.Items.Add("Pcs");
comboBoxSatuan.Items.Add("Kg");
comboBoxSatuan.Items.Add("Gram");
comboBoxSatuan.Items.Add(7000);
}
/**************************************************
* Membuat event Tombol "Simpan"
**************************************************/
private void btnSimpan_Click(object sender, EventArgs e)
{
/**************************************************
* Memeriksa apakah kolom Text kosong?
**************************************************/
if (textBoxKodeBarang.Text.Trim() == "" ||
textBoxNamaBarang.Text.Trim() == "" ||
textBoxHargaBeli.Text.Trim() == "" ||
textBoxHargaJual.Text.Trim() == "" ||
textBoxJumlah.Text.Trim() == "" ||
comboBoxSatuan.Text.Trim() == "")
{
MessageBox.Show("Mohon isikan terlebih dahulu kolom-kolom yang tersedia");
}
else
{
/**************************************************
* Simpan Data
**************************************************/
SqlConnection conn = Konn.GetConn();
try
{
cmd = new SqlCommand("INSERT INTO TBL_BARANG VALUES ('"+textBoxKodeBarang.Text+"','" + textBoxNamaBarang.Text + "','" + textBoxHargaBeli.Text + "','" + textBoxHargaJual.Text + "','" + textBoxJumlah.Text + "','" + comboBoxSatuan.Text + "')", conn);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Insert Data Berhasil!");
TampilBarang();
Bersihkan();
}
catch(Exception x)
{
MessageBox.Show("Tidak dapat menyimpan data");
}
conn.Close();
}
}
/**************************************************
* Membuat event Tombol "Update"
**************************************************/
private void btnUpdate_Click(object sender, EventArgs e)
{
/**************************************************
* Memeriksa apakah kolom Text kosong?
**************************************************/
if (textBoxKodeBarang.Text.Trim() == "" ||
textBoxNamaBarang.Text.Trim() == "" ||
textBoxHargaBeli.Text.Trim() == "" ||
textBoxHargaJual.Text.Trim() == "" ||
textBoxJumlah.Text.Trim() == "" ||
comboBoxSatuan.Text.Trim() == "")
{
MessageBox.Show("Mohon isikan terlebih dahulu kolom-kolom yang tersedia");
}
else
{
/**************************************************
* Update Data
**************************************************/
SqlConnection conn = Konn.GetConn();
try
{
cmd = new SqlCommand("UPDATE TBL_BARANG SET KodeBarang = '" + textBoxKodeBarang.Text + "', NamaBarang ='" + textBoxNamaBarang.Text + "', HargaBeli = '" + textBoxHargaBeli.Text + "', HargaJual = '" + textBoxHargaJual.Text + "', JumlahBarang = '" + textBoxJumlah.Text + "', SatuanBarang = '" + comboBoxSatuan.Text + "' WHERE KodeBarang = '" + textBoxKodeBarang.Text + "'", conn);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Update Data Berhasil!");
TampilBarang();
Bersihkan();
}
catch (Exception x)
{
MessageBox.Show("Tidak dapat mengupdate data");
}
conn.Close();
}
}
/**************************************************
* Membuat event Tombol "Hapus"
**************************************************/
private void btnHapus_Click(object sender, EventArgs e)
{
/**************************************************
* Memeriksa apakah kolom Text kosong?
**************************************************/
if (MessageBox.Show(textBoxNamaBarang.Text + ", yakin ingin dihapus?", "Hapus", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
/**************************************************
* Hapus Data
**************************************************/
SqlConnection conn = Konn.GetConn();
try
{
cmd = new SqlCommand("DELETE TBL_BARANG WHERE KodeBarang = '" + textBoxKodeBarang.Text + "'", conn);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Hapus Data Berhasil!");
TampilBarang();
Bersihkan();
}
catch (Exception x)
{
MessageBox.Show("Tidak dapat Menghapus data");
}
conn.Close();
}
else
{
}
}
private void textBoxCari_TextChanged(object sender, EventArgs e)
{
CariBarang();
}
private void btnRefresh_Click(object sender, EventArgs e)
{
Bersihkan();
}
}
}