I have datagridview datasource set to datatable source. At the end there is newrow so user can fill it in. However i have one issue. When user for instance fill first column and click enter then new row is not anymore new row and 'next' new row is added. What i want to achieve is if user starts to write something in new row then when he hit Enter before creating new row to check whether all columns has been firstly filled if not cancel adding new row. How to achieve that?
Asked
Active
Viewed 169 times
-1
-
1[RowValidating Event?](https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowvalidating(v=vs.110).aspx) – Pikoh May 24 '17 at 11:01
-
1You can set `dataGridView1.AllowUserToAddRows = false;` and then create an event for keypress where you validate whatever you want and if everything is ok `dataGridView1.Rows.Add();` or show an error message – FortyTwo May 24 '17 at 11:04
-
1@Pikoh Ok but how to in this event handler check current newrow if all columns cells have been filled out if yes continue to create newrow if not cancel? Could you show an example? – DinoDinn May 24 '17 at 11:10
-
1@DinoDinn did you follow the link i posted? you've got an example in there – Pikoh May 24 '17 at 11:10
1 Answers
0
You could do something like this:
public void checkIfRowIsEmpty()
{
foreach (DataGridViewRow rw in this.dataGridView1.Rows)
{
for (int i = 0; i < rw.Cells.Count; i++)
{
if (rw.Cells[i].Value == null || rw.Cells[i].Value == DBNull.Value || String.IsNullOrWhiteSpace(rw.Cells[i].Value.ToString())
{
dataGridView1.AllowUserToAddRows = false;
break;
}
else
{
dataGridView1.AllowUserToAddRows = true;
}
}
}
}
Call the methode when the user pushs "Enter".
Here is an example: Enter key pressed event handler

Julez
- 65
- 2
- 8