如何使用按钮向DataGridView添加新行 [英] How to add a new row to DataGridView using a button
本文介绍了如何使用按钮向DataGridView添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#Windows应用程序中,我使用DataGridView显示所有数据。当用户单击添加新行按钮时,我想向网格添加一个新行。如何实现这一点?
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?
推荐答案
在Windows窗体DataGridView控件中操作行: http://msdn.microsoft.com/en-us/library/ddtce152.aspx
Manipulate Rows in the Windows Forms DataGridView Control : http://msdn.microsoft.com/en-us/library/ddtce152.aspx
点击按钮添加行通过使用dataGridView.Rows.Add如下所示例子
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
}
这篇关于如何使用按钮向DataGridView添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文