//Add Item button code, pops a new instance of AddItem form private void btnAddItem_Click(object sender, EventArgs e) { //used "using" to ensure better consumption of system resources using (var frmAddItem = new frmAddItem(this)) { frmAddItem.ShowDialog(); } }
//click event on datagridview public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { //Edit button if (e.ColumnIndex == dataGridView1.Columns["Edit"].Index && e.RowIndex >= 0) { //TODO - when clicked, using the data grid view data, send the values to a new frmAddItem //used "using" to ensure better consumption of system resources using (var frmAddItem = new frmAddItem(this)) { ExpenseItem expenseItem = ExpenseItemsDB.GetExpenseItem(System.Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString())); frmAddItem.SendDataToForm2(expenseItem); frmAddItem.ShowDialog(); //how do I get the datagrid values from the row into frmAddItem and then return them to same row? //Do I need a new method to pass the row to an expense object on frmMain, then pass to frmAddItem inputs? //ExpenseItemsDB.GetExpenseItem(System.Convert.ToInt32(this.idDataGridViewTextBoxColumn.ToString())); frmAddItem.SendDataToForm2(expenseItem); this.tblExpenseItemsBindingSource.EndEdit(); this.tbl_ExpenseItemsTableAdapter.Update(dat_ExpenseItems); } } //Delete button if (e.ColumnIndex == dataGridView1.Columns["Delete"].Index && e.RowIndex >= 0) { // Initializes the variables to pass to the MessageBox.Show method. string message = "By clicking yes, this expense item will be permanently removed from your record. Are you sure that you want to delete this?"; string caption = "Permanent Delete Warning"; MessageBoxButtons buttons = MessageBoxButtons.YesNo; DialogResult result; // Displays the MessageBox. result = MessageBox.Show(message, caption, buttons); //if yes is clicked, proceed with deletion if (result == System.Windows.Forms.DialogResult.Yes) { int rowIndex = e.RowIndex; dataGridView1.Rows.RemoveAt(rowIndex); dat_ExpenseItems.tbl_ExpenseItems.Rows[rowIndex].Delete(); tbl_ExpenseItemsTableAdapter.Update(dat_ExpenseItems);//****data reappearing when app loaded again. this.tblExpenseItemsBindingSource.EndEdit(); } } }