Reputation: 2795
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:
Upvotes: 9
Views: 21635
Reputation: 31
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;
}
Upvotes: 3
Reputation: 327
I know this is old. The easiest way is, uncheck "Enable Adding" from the design view
Upvotes: 1
Reputation: 1493
This is how it is achieved.
a) You can check contents of current row in RowLeave event using
String.IsNullOrWhiteSpace(GridPGLog.Rows[e.RowIndex].Cells[0].value.toString())
using (or) Cells[0] || cells[1] || cell[2] || ..
if any error is found, set focus to the error cell and force user to enter data.
DataGridViewRow rowToSelect = this.dgvJobList.CurrentRow;
rowToSelect.Selected = true;
rowToSelect.Cells[0].Selected = true;
this.dgvJobList.CurrentCell = rowToSelect.Cells[0];
b) or you can place a Save button and check all newly added rows using a foreach loop
Upvotes: 0
Reputation: 3138
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());
}
}
}
Upvotes: 5
Reputation: 6149
I think on the event CellClick you can check in which column you are and then add a new row, something like : DataGridView1.Rows.Add()
Upvotes: 0