• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5511
  • Last Modified:

How to programmically add multiple rows to datagridview

I need sample code to programmically add a predeterminded number of rows to a bound datagridview with a click event.
0
dave_sky
Asked:
dave_sky
  • 2
1 Solution
 
DhaestCommented:
Did you already succeed to add 1 row ?
Did you try:
Datagridview.rows.add("","","","")
(add all the parameters as an empty string, 0, ... (default values)

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx
0
 
dave_skyAuthor Commented:
Dhaest,

>>Did you already succeed to add 1 row ?
Yes.

I get this error message when I use the sample code from ms:
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
0
 
SwissKnifeCommented:
Hello Dhaest
If you have a data bound DataGridView, then you have probably a tableAdapter. I think you can add rows throug the tableAdaptor, and the Tableadaptor updates the dataGridView
0
 
DhaestCommented:
Rows cannot be added programatically to the datagridview's collection when control is databound
Solution: To add a row to a databound DataGridView, add the row to the underlying data source (e.g., DataTable or BindingSource).

Example below

DataRow dr;
DataSet ds; // this is bound to DataGridView
 
// some logic setting up dataset and tables within
 
dr = ds.Tables[0].NewRow(); // Table[0] of ds is the data I want to display
dr["Column1"] = "Finally";
dr["Column2"] = "Got";
dr["Column3"] = "this";
dr["Column4"] = "Working";
 
// add row to dataset now
ds.Tables[0].Rows.Add(dr);

Open in new window

0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now