private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { if (e.Row.Index < this.CanData.Count) { // If the user has deleted an existing row, remove the // corresponding CANDATA object from the data store. this.CanData.RemoveAt(e.Row.Index); } if (e.Row.Index == this.rowInEdit) { // If the user has deleted a newly created row, release // the corresponding CANDATA object. this.rowInEdit = -1; this.CANDATAInEdit = null; } }
private void dataGridView1_CellValuePushed(object sender, DataGridViewCellValueEventArgs e) { CANDATA CANDATATmp = null; // Store a reference to the CANDATA object for the row being edited. if (e.RowIndex < this.CanData.Count) { // If the user is editing a new row, create a new CANDATA object. if (this.CANDATAInEdit == null) { this.CANDATAInEdit = new CANDATA( ((CANDATA)this.CanData[e.RowIndex]).CompanyName, ((CANDATA)this.CanData[e.RowIndex]).ContactName); } CANDATATmp = this.CANDATAInEdit; this.rowInEdit = e.RowIndex; } else { CANDATATmp = this.CANDATAInEdit; } // Set the appropriate CANDATA property to the cell value entered. String newValue = e.Value as String; switch (this.dataGridView1.Columns[e.ColumnIndex].Name) { case "Company Name": CANDATATmp.CompanyName = newValue; break; case "Contact Name": CANDATATmp.ContactName = newValue; break; } }
private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e) { // Save row changes if any were made and release the edited // CANDATA object if there is one. if (e.RowIndex >= this.CanData.Count && e.RowIndex != this.dataGridView1.Rows.Count - 1) { // Add the new CANDATA object to the data store. this.CanData.Add(this.CANDATAInEdit); this.CANDATAInEdit = null; this.rowInEdit = -1; } else if (this.CANDATAInEdit != null && e.RowIndex < this.CanData.Count) { // Save the modified CANDATA object in the data store. this.CanData[e.RowIndex] = this.CANDATAInEdit; this.CANDATAInEdit = null; this.rowInEdit = -1; } else if (this.dataGridView1.ContainsFocus) { this.CANDATAInEdit = null; this.rowInEdit = -1; } }
private void dataGridView1_NewRowNeeded(object sender, DataGridViewRowEventArgs e) { // Create a new CANDATA object when the user edits // the row for new records. this.CANDATAInEdit = new CANDATA(); this.rowInEdit = this.dataGridView1.Rows.Count - 1; }
private void dataGridView1_CancelRowEdit(object sender, QuestionEventArgs e) { if (this.rowInEdit == this.dataGridView1.Rows.Count - 2 && this.rowInEdit == this.CanData.Count) { // If the user has canceled the edit of a newly created row, // replace the corresponding CANDATA object with a new, empty one. this.CANDATAInEdit = new CANDATA(); } else { // If the user has canceled the edit of an existing row, // release the corresponding CANDATA object. this.CANDATAInEdit = null; this.rowInEdit = -1; } }