Click here to Skip to main content
15,945,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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
Updated 8-Jun-16 21:01pm

Try this:
C#
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:
   index++;
   this.dataGridView1.Rows.Add();
}

Refer more details:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowsadded.aspx[^]
http://msdn.microsoft.com/en-us/library/5s3ce6k8.aspx[^]
 
Share this answer
 
v2
Comments
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);
Please this code given bellow
to add blank row into the data grid view
C#
DataGridView.Rows.Add()
.
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

C#
DataGridView.Rows.Add("ItemName","ItemCode","Quantity",)
 
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