private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { try { if (dataGridView1.Rows.Count > 1) { if (string.IsNullOrEmpty(dataGridView1.CurrentRow.Cells[Column1.Name].Value.ToString())) { dataGridView1.AllowUserToAddRows = false; } else { dataGridView1.AllowUserToAddRows = true; } } } catch (NullReferenceException) { } catch (InvalidOperationException) { } }
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (dataGridView1.CurrentCell.ColumnIndex == Column1.Index) { if (e.Control is TextBox) { txtCol1 = e.Control as TextBox; txtCol1.TextChanged -= new EventHandler(txtCol1_TextChanged); txtCol1.TextChanged += new EventHandler(txtCol1_TextChanged); } } else if (dataGridView1.CurrentCell.ColumnIndex == Column2.Index) { if (e.Control is TextBox) { txtCol2 = e.Control as TextBox; txtCol2.TextChanged -= new EventHandler(txtCol2_TextChanged); txtCol2.TextChanged += new EventHandler(txtCol2_TextChanged); } } //else if... textbox event for another column } private void txtCol2_TextChanged(object sender, EventArgs e) { if (dataGridView1.CurrentCell.ColumnIndex == Column2.Index) { try { if (dataGridView1.CurrentRow.Cells[Column1.Name].Value.ToString() == "") { dataGridView1.AllowUserToAddRows = false; } else { dataGridView1.AllowUserToAddRows = true; } } catch (NullReferenceException) { } catch (InvalidOperationException) { } } } private void txtCol1_TextChanged(object sender, EventArgs e) { if (dataGridView1.CurrentCell.ColumnIndex == Column1.Index) { if (txtCol1.Text == "") { dataGridView1.AllowUserToAddRows = false; } else { dataGridView1.AllowUserToAddRows = true; } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)