Click here to Skip to main content
12,759,844 members (30,851 online)
Rate this:
Please Sign up or sign in to vote.
See more: VS2008 C# Visual-Studio , +
How to add a row in datagridview in winform application.
Scenario is:
- I've one grid on windows form.
- Grid's columns are pre-definded. (Here, I mean column index, name , header text and order is not changeable at runtime.)
- I need to add new row in that grid.
Anyone, help me in this regard. How can I do this task ?
Posted 10-Jul-12 22:03pm
Updated 8-Jun-16 22:01pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try this:
private void InsertNewRowToDGV()
   //we have to get the index of a last row id:
   int index = this.dataGridView1.Rows.Count;

   //and count +1 to get a new row id:

Refer more details:[^][^]
Itz.Irshad 11-Jul-12 3:26am
DataGridViewRow dgvRow = new DataGridViewRow();
dgvRow.Cells[0].Value = objBook.ID;
dgvRow.Cells[1].Value = objBook.Title;
dgvRow.Cells[2].Value = objBook.Author;
dgvRow.Cells[3].Value = objBook.Genre;
dgvRow.Cells[4].Value = objBook.Price;
dgvRow.Cells[5].Value = objBook.PublishDate;
dgvRow.Cells[6].Value = objBook.Description;
int totalrows = myMainApplication.dgvBooksDetails.Rows.Count;
myMainApplication.dgvBooksDetails.Rows.Insert(totalrows++, dgvRow);

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Got This Error:
pradiprenushe 11-Jul-12 3:52am
I think index increment will not require. Pleas check this
myMainApplication.dgvBooksDetails.Rows.Insert(totalrows, dgvRow);
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Please this code given bellow
to add blank row into the data grid view
Or Can you use this coding also

Suppose I have a DataGridView having three columns named ItemName,Itemcode and ItemQuantity
for that you can use following code given bellow


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.170217.1 | Last Updated 9 Jun 2016
Copyright © CodeProject, 1999-2017
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