• We just launched and are currently in beta. Join us as we build and grow the community.

Adding Multiple Columns and Rows in the DataGridView Programmatically Using C#.

rolanstates2212

Historical OSINT Analyst
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
56
Likes
194
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This time, I’m going to teach you how to add multiple columns and rows in the Datagridview programmatically using C#. This method will illustrate on how to control the columns and rows by creating it manually inside the datagridview. This is a big help for you when you are a beginner in programming.

Creating an Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in C#.
creating_application_c_7.png

Step 2

Do the form just like shown below.
2019-04-30_2.png

Step 3

Press F7 to open the code editor. In the code editor, create a method for adding multiple columns and rows in the datagridview

  1. private

    void

    add_Columns_Rows(

    )
  2. {
  3. //setup the columns to be added.
  4. dataGridView1.

    ColumnCount

    =

    5

    ;
  5. //Set the columns name
  6. dataGridView1.

    Columns

    [

    0

    ]

    .

    Name

    =

    "ID"

    ;
  7. dataGridView1.

    Columns

    [

    1

    ]

    .

    Name

    =

    "Item"

    ;
  8. dataGridView1.

    Columns

    [

    2

    ]

    .

    Name

    =

    "Price"

    ;
  9. dataGridView1.

    Columns

    [

    3

    ]

    .

    Name

    =

    "Quantity"

    ;
  10. dataGridView1.

    Columns

    [

    4

    ]

    .

    Name

    =

    "Sub-Total"

    ;

  11. //Set a value to be added in a row
  12. string

    [

    ]

    row =

    new

    string

    [

    ]

    {

    "1"

    , "Cellphone"

    , "15,000"

    , "2"

    , "30,000"

    }

    ;
  13. //adding rows in the datagridview
  14. dataGridView1.

    Rows

    .

    Add

    (

    row)

    ;
  15. //Set a value to be added in a row
  16. row =

    new

    string

    [

    ]

    {

    "2"

    , "Laptop"

    , "21,000"

    , "1"

    , "21,000"

    }

    ;
  17. //adding rows in the datagridview
  18. dataGridView1.

    Rows

    .

    Add

    (

    row)

    ;
  19. //Set a value to be added in a row
  20. row =

    new

    string

    [

    ]

    {

    "3"

    , "Speaker"

    , "2,000"

    , "2"

    , "4,000"

    }

    ;
  21. //adding rows in the datagridview
  22. dataGridView1.

    Rows

    .

    Add

    (

    row)

    ;
  23. //Set a value to be added in a row
  24. row =

    new

    string

    [

    ]

    {

    "4"

    , "Desktop Computer"

    , "10,000"

    , "5"

    , "50,000"

    }

    ;
  25. //adding rows in the datagridview
  26. dataGridView1.

    Rows

    .

    Add

    (

    row)

    ;
  27. }

Step 4

Do the following codes to execute the method that you have created in the first load of the form.

  1. private

    void

    Form1_Load(

    object

    sender, EventArgs e)
  2. {
  3. //displaying the columns and rows.
  4. add_Columns_Rows(

    )

    ;
  5. }

For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below

 

450,270

322,965

322,974

Top