1 Replies - 106152 Views - Last Post: 10 December 2007 - 08:40 PM Rate Topic: -----

#1 johnsvakel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-December 07

adding rows to a data grid view

Posted 10 December 2007 - 08:20 PM

How can we add rows to a datagridview programmatically, where the gridview is already bound to a data source?
Is This A Good Question/Topic? 0
  • +

Replies To: adding rows to a data grid view

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: adding rows to a data grid view

Posted 10 December 2007 - 08:40 PM

One method is to take a string array with all the values that are contained in the row and insert it into the DataGridView using the Rows.Add method.

The following example loads multiple rows into the DataGridView.
Example from MSDN:
		// Populate the rows.
		string[] row1 = new string[]{"Meatloaf", 
											"Main Dish", boringMeatloaf, boringMeatloafRanking};
		string[] row2 = new string[]{"Key Lime Pie", 
											"Dessert", "lime juice, evaporated milk", "****"};
		string[] row3 = new string[]{"Orange-Salsa Pork Chops", 
											"Main Dish", "pork chops, salsa, orange juice", "****"};
		string[] row4 = new string[]{"Black Bean and Rice Salad", 
											"Salad", "black beans, brown rice", "****"};
		string[] row5 = new string[]{"Chocolate Cheesecake", 
											"Dessert", "cream cheese", "***"};
		string[] row6 = new string[]{"Black Bean Dip", "Appetizer",
											"black beans, sour cream", "***"};
		object[] rows = new object[] { row1, row2, row3, row4, row5, row6 };

		foreach (string[] rowArray in rows)
		{
			dataGridView.Rows.Add(rowArray);
		}


Example only loading one row at a time:
// Populate the new row with data
string[] rowArray = new string[]{"Meatloaf", 
								 "Main Dish", boringMeatloaf, boringMeatloafRanking};

//Add the new row to the DataGridView
dataGridView.Rows.Add(rowArray[0]);


You can find out more information at the following link to the MSDN.

Manipulate Rows in a DataGridView.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1