如何使用按钮添加一个新行到的DataGridView [英] How to add a new row to DataGridView using a button

查看:139
本文介绍了如何使用按钮添加一个新行到的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

按钮的onclick添加行

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆