Click here to Skip to main content
Sign Up to vote bad
good
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 - 21:03


2 solutions

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:
   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[^]
  Permalink  
Comments
Itz.Irshad - 11 Jul '12 - 3:26
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:52
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
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
DataGridView.Rows.Add("ItemName","ItemCode","Quantity",)
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 545
1 Maciej Los 270
2 Slacker007 240
3 OriginalGriff 235
4 Aarti Meswania 185
0 Sergey Alexandrovich Kryukov 8,953
1 OriginalGriff 7,134
2 CPallini 3,758
3 Rohan Leuva 3,066
4 Maciej Los 2,528


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 11 Jul 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid