Results 1 to 5 of 5

Thread: [RESOLVED] Add two arrays to datagrid view

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    Resolved [RESOLVED] Add two arrays to datagrid view

    I have two arrays xdat and ydat and I want to display them into datagridview.

    I tried to use
    Code:
    DataGridView1.Rows.Add(jdxDat, jdyDat)
    but I got the error "No row can be added to a Data grid view control that doesn't have columns. Columns must be added first".

    Can you suggest how to add rows ?

    For example my jdxData looks like
    Code:
     jdxDat = {0,1,2,3,4,5}
    and
    Code:
    jdyDat = {5,10, 11,19, 18,21}
    Thanks for any help.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Add two arrays to datagrid view

    Did you even read that error message? It tells you exactly what to do.
    Columns must be added first
    what part of that is unclear?

    Also, you can't add the whole array to one row. You need to use a loop and add one element from each array to each row.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    Re: Add two arrays to datagrid view

    Thanks. I figured it out.

    Code:
                DataGridView1.ColumnCount = 2
                For i = 0 To jdxDat.Length - 1
                    DataGridView1.Rows.Add(jdxDat(i), jdyDat(i))
                Next
    How do I add header to this datagridview ? For example on column1 I want to write "distance", and column2 I want to write "value".

    Thanks.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Add two arrays to datagrid view

    You should be adding the columns in the designer, where you can set the header text and more besides.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2011
    Posts
    16

    Re: Add two arrays to datagrid view

    Thanks so much for your time and help. I was able to figure that out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width