-
Notifications
You must be signed in to change notification settings - Fork 0
/
Daftar Pegawai GridView.cs
205 lines (181 loc) · 6.68 KB
/
Daftar Pegawai GridView.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace kelompok
{
public partial class Daftar_Pegawai_GridView : Form
{
private bool isEditMode = false;
public Daftar_Pegawai_GridView()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Simpan")
{
UpdateData();
}
else
{
TambahData();
}
}
private void TambahData()
{
string kodePegawai = textBox1.Text;
string namaPegawai = textBox2.Text;
string jenisKelamin = radioButton1.Checked ? "Pria" : "Wanita";
string departemen = comboBox1.SelectedItem?.ToString() ?? "";
if (string.IsNullOrEmpty(kodePegawai) || string.IsNullOrEmpty(namaPegawai) || string.IsNullOrEmpty(departemen))
{
MessageBox.Show("Harap isi semua field dengan benar.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
using (StreamWriter sw = new StreamWriter("Pegawai.txt", true))
{
sw.WriteLine($"{kodePegawai}|{namaPegawai}|{jenisKelamin}|{departemen}");
}
dataGridView1.Rows.Add(kodePegawai, namaPegawai, jenisKelamin, departemen);
ClearInputFields();
MessageBox.Show("Data pegawai berhasil ditambahkan.", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UpdateData()
{
string kodePegawai = textBox1.Text;
string namaPegawai = textBox2.Text;
string jenisKelamin = radioButton1.Checked ? "Pria" : "Wanita";
string departemen = comboBox1.SelectedItem?.ToString() ?? "";
try
{
UpdateDataDiFile(kodePegawai, namaPegawai, jenisKelamin, departemen);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells["kode"].Value.ToString() == kodePegawai)
{
row.Cells["nama"].Value = namaPegawai;
row.Cells["kelamin"].Value = jenisKelamin;
row.Cells["departemen"].Value = departemen;
break;
}
}
ClearInputFields();
isEditMode = false;
button1.Text = "Tambah";
MessageBox.Show("Data pegawai berhasil diupdate.", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
void UpdateDataDiFile(string kodePegawai, string namaPegawai, string jenisKelamin, string departemen)
{
try
{
string[] lines = File.ReadAllLines("Pegawai.txt");
for (int i = 0; i < lines.Length; i++)
{
string[] data = lines[i].Split('|');
if (data[0] == kodePegawai)
{
lines[i] = $"{kodePegawai}|{namaPegawai}|{jenisKelamin}|{departemen}";
break;
}
}
File.WriteAllLines("Pegawai.txt", lines);
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button2_Click(object sender, EventArgs
e)
{
TampilkanData("");
}
private void button3_Click(object sender, EventArgs e)
{
TampilkanData("Pria");
}
private void button4_Click(object sender, EventArgs e)
{
TampilkanData("Wanita");
}
private void TampilkanData(string filterJenisKelamin)
{
dataGridView1.Rows.Clear();
try
{
string[] lines = File.ReadAllLines("Pegawai.txt");
foreach (string line in lines)
{
string[] data = line.Split('|');
if (string.IsNullOrEmpty(filterJenisKelamin) || data[2] == filterJenisKelamin)
{
dataGridView1.Rows.Add(data[0], data[1], data[2], data[3]);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ClearInputFields()
{
textBox1.Clear();
textBox2.Clear();
radioButton1.Checked = false;
radioButton2.Checked = false;
comboBox1.SelectedIndex = -1;
textBox1.Focus();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
string kodePegawai = selectedRow.Cells["kode"].Value.ToString();
string namaPegawai = selectedRow.Cells["nama"].Value.ToString();
string jenisKelamin = selectedRow.Cells["kelamin"].Value.ToString();
string departemen = selectedRow.Cells["departemen"].Value.ToString();
textBox1.Text = kodePegawai;
textBox2.Text = namaPegawai;
if (jenisKelamin == "Pria")
{
radioButton1.Checked = true;
}
else
{
radioButton2.Checked = true;
}
comboBox1.SelectedItem = departemen;
isEditMode = true;
button1.Text = "Simpan";
}
}
private void Daftar_Pegawai_GridView_Load(object sender, EventArgs e)
{
TampilkanData("");
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}