Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,172 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,914 people online right now. Registration is fast and FREE... Join Now!




adding rows to a data grid view

 
Reply to this topicStart new topic

adding rows to a data grid view

johnsvakel
10 Dec, 2007 - 07:20 PM
Post #1

New D.I.C Head
*

Joined: 6 Dec, 2007
Posts: 5


My Contributions
How can we add rows to a datagridview programmatically, where the gridview is already bound to a data source?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Adding Rows To A Data Grid View
10 Dec, 2007 - 07:40 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,926



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
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:
CODE

        // 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:
CODE

// 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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:40AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month