Click here to Skip to main content
12,069,536 members (64,762 online)
Rate this:
 
Please Sign up or sign in to vote.
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,
Smile | :)
Posted 20-Mar-11 1:15am
Edited 20-Mar-11 4:09am
v2
Comments
Marcus Kramer 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try the DataGridView.Rows.Add[^] method!
  Permalink  
Comments
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

I had same issue : how to have the blank row of the DataGrid to
have computed values. I posted the question, and answer here :

http://stackoverflow.com/questions/12485664/set-the-content-of-a-new-row-datagrid[^]

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160208.1 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100