Click here to Skip to main content
11,431,483 members (69,124 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VS2008 C# DataGridView
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
Rate this: bad
good
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:
   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  
v2
Comments
Itz.Irshad at 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 at 11-Jul-12 3:52am
   
I think index increment will not require. Pleas check this
myMainApplication.dgvBooksDetails.Rows.Insert(totalrows, dgvRow);
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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)



Advertise | Privacy | Mobile
Web02 | 2.8.150428.2 | Last Updated 11 Jul 2012
Copyright © CodeProject, 1999-2015
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