public FrmTambahData(bool addMode, AdressBook addrBook = null) { InitializeComponent(); _addMode = addMode; if (addrBook != null) { _addrBook = addrBook; this.txtNama.Text = addrBook.Nama; this.txtAlamat.Text = addrBook.Alamat; this.txtKota.Text = addrBook.Kota; this.txtNoHp.Text = addrBook.NoHp; this.dtpTglLahir.Value = addrBook.TanggalLahir.Date; this.txtEmail.Text = addrBook.Email; } }
private void btnEdit_Click(object sender, EventArgs e) { if (this.dgvData.SelectedRows.Count > 0) { DataGridViewRow row = this.dgvData.SelectedRows[0]; AdressBook addrBook = new AdressBook(); 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(); FrmTambahData form = new FrmTambahData(false, addrBook); if (form.Run(form)) { FrmAddressBook_Load(null, null); } } }
private void btnHapus_Click(object sender, EventArgs e) { if (this.dgvData.SelectedRows.Count > 0 && MessageBox.Show("delete selected rows? ? ", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { DataGridViewRow row = this.dgvData.SelectedRows[0]; AdressBook addrBook = new AdressBook(); 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(); try { 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) { // do nothing } else { writer.WriteLine(line); } } } } File.Delete("addressbook.csv"); File.Move("temporary.csv", "addressbook.csv"); FrmAddressBook_Load(null, null); } catch (Exception ex) { MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } } }