private void btnTambah_Click(object sender, EventArgs e) { Tambahdata form = new Tambahdata(true); if (form.Run(form)) { LoadData(); } }
private void btnEdit_Click(object sender, EventArgs e) { if (this.dgvData.SelectedRows.Count > 0) { DataGridViewRow row = this.dgvData.SelectedRows[0]; people addrBook = new people(); addrBook.Nama = row.Cells[0].Value.ToString(); addrBook.Alamat = row.Cells[1].Value.ToString(); addrBook.Kota = row.Cells[2].Value.ToString(); addrBook.NoHP = row.Cells[3].Value.ToString(); addrBook.TanggalLahir = Convert.ToDateTime(row.Cells[4].Value).Date; addrBook.Email = row.Cells[5].Value.ToString(); Tambahdata form = new Tambahdata(false, addrBook); if (form.Run(form)) { LoadData(); } } }
private void btnSimpan_Click(object sender, EventArgs e) { // validase Addressbook form = new Addressbook(); Tambahdata close = new Tambahdata(true); if (this.txtNama.Text.Trim() == "") { MessageBox.Show("nama wajib diisi ... ", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); this.txtNama.Focus(); } else if (this.txtAlamat.Text.Trim() == "") { MessageBox.Show("Alamat wajib diisi ... ", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); this.txtAlamat.Focus(); } else if (this.txtKota.Text.Trim() == "") { MessageBox.Show("Kota wajib diisi ... ", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); this.txtKota.Focus(); } else if (this.txtNoHP.Text.Trim() == "") { MessageBox.Show("No HP wajib diisi ... ", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); this.txtNoHP.Focus(); } else if (this.txtEmail.Text.Trim() == "") { MessageBox.Show("Email wajib diisi ... ", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); this.txtEmail.Focus(); } else { try { // simpan data ke file if (_addMode) { using (var fs = new FileStream("addressbook.csv ", FileMode.Append, FileAccess.Write)) // file Stream untuk menangani data { using (StreamWriter writer = new StreamWriter(fs)) { writer.WriteLine($"{txtNama.Text.Trim()};{txtAlamat.Text.Trim()};{txtKota.Text.Trim()};{txtNoHP.Text.Trim()};{dtpTglLahir.Value.ToShortDateString()};{txtEmail.Text.Trim()}"); // nama;alamat, .... } } } else // edit data { string[] fileContent = File.ReadAllLines("addressbook.csv"); using (FileStream fs = new FileStream("temporary.csv", FileMode.Create, FileAccess.Write)) { using (StreamWriter writer = new StreamWriter(fs)) { foreach (string line in fileContent) { string[] arrline = line.Split(';'); if (arrline[0] == _addrBook.Nama && arrline[1] == _addrBook.Alamat && arrline[2] == _addrBook.Kota && arrline[3] == _addrBook.NoHP && Convert.ToDateTime(arrline[4]).Date == _addrBook.TanggalLahir.Date && arrline[5] == _addrBook.Email) { writer.WriteLine($"{txtNama.Text.Trim()};{txtAlamat.Text.Trim()};{txtKota.Text.Trim()};{txtNoHP.Text.Trim()};{dtpTglLahir.Value.ToShortDateString()};{txtEmail.Text.Trim()}"); } else { writer.WriteLine(line); } } } } File.Delete("addressbook.csv"); File.Move("temporary.csv", "addressbook.csv"); } _result = true; this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
public bool Run(Tambahdata form) { form.ShowDialog(); return(_result); }