Click here to Skip to main content
15,665,942 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
Hi all,

I have a DataGridView where I am setting its AllowUserToAddRow property to true,
When using keyboard for input, it works and give me a new row,
but when trying to fill the row using the code like this :
dataGridView1.Rows[dataGridView1.RowCount - 1].Cells["ID"].Value = "1";

It fill it with data but without adding a new row.

Please tell me how to do this, I couldn't do it manually as it adds the new row before my filled row data.

Thanks in advance,
Updated 6-Aug-19 4:33am
fjdiewornncalwe 20-Mar-11 9:09am    
Based on your comment to Griff's answer below, I have taken the liberty of updating your question subject to include the "pre-population" so that your question can attract more appropriate answers.

Try the DataGridView.Rows.Add[^] method!
Share this answer
Michael Waguih 20-Mar-11 6:34am    
Thanks, but I have no problem with the way to add anew row, my problem is how to make the new row filled with data from the code lose its property of being a new line and add an empty new one at the end of the dataGridView.
This will do it for an unbound grid with three columns
in repsonse to a button click or some other event where
the user has not added the row directly to the grid

private void addRowButton_Click(object sender, EventArgs e)
    int rowIndex = dataGridView1.Rows.Add();
    DataGridViewRow row = dataGridView1.Rows[rowIndex];
    row.Cells[0].Value = "test1";
    row.Cells[1].Value = "test2";
    row.Cells[2].Value = "test3";
    dataGridView1.CurrentCell = row.Cells[0];

When the user does add a row to the grid by entering
data in one of the columns on the new row line the
following will work

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
    DataGridViewRow row = dataGridView1.Rows[e.RowIndex - 1];
    row.Cells[0].Value = "test1";
    row.Cells[1].Value = "test2";
    row.Cells[2].Value = "test3";

Note that any user input will override values set here. This can be avoided by making
columns read only where required.
Share this answer
I had same issue : how to have the blank row of the DataGrid to
have computed values. I posted the question, and answer here :[^]

i copy the answer here :

When a DataTable is bound to a DataGrid, a CollectionView is created in order to see it. You can get it by using the (static/shared) CollectionView.GetDefaultView(ThePropertyThatIsBound) method.
Since it implements ICollectionChanged, you can add an event handler to the CollectionChangedEvent.

In the CollectionChanged event handler, if you have a new item (e.NewItems.Count>0) you must check it against System.Windows.Data.CollectionView.NewItemPlaceholder and if it is not a place holder, then it is a brand new item, for wich i can set all default values.
Share this answer
You cannot modify the cells in the row in the UserAddedRow event. Use DefaultValuesNeeded[^] instead.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900