I have a DataGridView in my WinForm application in C# 3.5.
AllowUserToAddNewRow
property is set true. When user types any text in DataGridView, another new row is automatically added to DataGridView. I do not want this new row added until some checks are performed on the current row and all the necessary information has been filled in there.
Example : I have a DataGridView with a blank row:
When I begin typing a new row is added, which is too soon:
What I want is that the new row be added only after the user enters a Quantity:
Simply set the DataGridView. AllowUserToAddRows property to false .
The DataGridViewRow class represents a row in a DataGridView control. You can retrieve rows through the Rows and SelectedRows collections of the control. Unlike a DataGridViewColumn, a DataGridViewRow physically contains a collection of all of the cells in that row.
Set the column's ReadOnly property to true to make it non-editable. And alter its DefaultCellStyle. BackColor (and/or ForeColor) to make it obvious to the user.
Set AllowUserToAddNewRow = false Now add a blank row initially to your datasource for eg. if you are binding the DataGridView to a DataTable called DT then just before
dataGridView1.DataSource = DT;
Do something like
DT.Rows.Add(DT.NewRow());
This is to have one blank row initially so that the first record can be entered. Then handle the event dataGridView1.CellEndEdit, in that event write something like this:
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)//The index of your Quantity Column
{
int qty = (int)DT.Rows[e.RowIndex][e.ColumnIndex];
if (qty > 0)//Your logic if required
{
DT.Rows.Add(DT.NewRow());
}
}
}
Basically it's a simple play with some events and enabling/disabling the AllowUserToAddRow property:
public Form1()
{
InitializeComponent();
//creating a test DataTable and adding an empty row
DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Rows.Add(dt.NewRow());
//binding to the gridview
dataGridView1.DataSource = dt;
//Set the property AllowUserToAddRows to false will prevent a new empty row
dataGridView1.AllowUserToAddRows = false;
}
Now the events... When the cell recognize the editing it will fire a event called CellBeginEdit. When it's in editing mode set the AllowUserToAddRows to false
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
}
When the cell recognize the end of editing it will fire a event called CellEndEdit. When it ends the editing mode check for your conditions. Based on the result set the AllowUserToAddRows to true ot keep it false.
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//instead of MessageBox there could be as well your check conditions
if (MessageBox.Show("Cell edit finished, add a new row?", "Add new row?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
dataGridView1.AllowUserToAddRows = true;
else dataGridView1.AllowUserToAddRows = false;
}
I know this is old. The easiest way is, uncheck "Enable Adding" from the design view
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With