如何使用按钮向DataGridView添加新行

时间:2010-07-01 06:32:33

标签: c# .net winforms datagridview

在C#Windows应用程序中,我使用DataGridView显示所有数据。我想在用户单击“添加新行”按钮时向网格添加新行。我怎么能做到这一点?

2 个答案:

答案 0 :(得分:3)

在Windows窗体DataGridView控件中操作行:http://msdn.microsoft.com/en-us/library/ddtce152.aspx

按钮onclick使用dataGridView.Rows.Add添加行,如下例所示

 // 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 
        }

答案 1 :(得分:0)

在你的onclick事件中,你是在调用DataTable.Rows.Add()还是类似的?