In a C# Windows application开发者_运维问答, I'm using a DataGridView to display all the data. I want to add a new row to the grid when the user clicks the Add New Row button. How can I accomplish this?
Manipulate Rows in the Windows Forms DataGridView Control : http://msdn.microsoft.com/en-us/library/ddtce152.aspx
onclick of button add row by using dataGridView.Rows.Add as shown below in example
// 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); // addding row
}
In your onclick event, are you calling DataTable.Rows.Add() or similar?
精彩评论