Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. A problem with an array populating an unbound DGV

A problem with an array populating an unbound DGV

Scheduled Pinned Locked Moved Visual Basic
helpquestiondatabasedata-structures
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DA_Loring
    wrote on last edited by
    #1

    THe error message is: "Index and length must refer to a location within the string. Parameter name: length" Source: mscorlib The lines causing the error are: Dim row() As String = {rowBuild(0), rowBuild(1), rowBuild(2), rowBuild(3), rowBuild(4), rowBuild(5), rowBuild(6), rowBuild(7), rowBuild(8), rowBuild(9), rowBuild(10), rowBuild(11), rowBuild(12), rowBuild(13), rowBuild(14), rowBuild(15)} Me.dgvData.Rows.Add(row) This is loading data into an unbound DataGridView. The declaration for rowBuild is Dim rowBuild(15) As String Each rowBuild element has data in it, so I can't see what is wrong. Any ideas????????:confused:

    David Loring !! Keep Music Live !!

    D 1 Reply Last reply
    0
    • D DA_Loring

      THe error message is: "Index and length must refer to a location within the string. Parameter name: length" Source: mscorlib The lines causing the error are: Dim row() As String = {rowBuild(0), rowBuild(1), rowBuild(2), rowBuild(3), rowBuild(4), rowBuild(5), rowBuild(6), rowBuild(7), rowBuild(8), rowBuild(9), rowBuild(10), rowBuild(11), rowBuild(12), rowBuild(13), rowBuild(14), rowBuild(15)} Me.dgvData.Rows.Add(row) This is loading data into an unbound DataGridView. The declaration for rowBuild is Dim rowBuild(15) As String Each rowBuild element has data in it, so I can't see what is wrong. Any ideas????????:confused:

      David Loring !! Keep Music Live !!

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Using the code you've supplied, I can't duplicate the problem. I actually can't BREAK it in any way unless I fail to define the columns in the DGV before I add the rows.

      With DGV1.Columns
      .Clear()
      .Add("Col1", "Column Header 1")
      .Add("Col2", "Column Header 2")
      .Add("Col3", "Column Header 3")
      End With

      Dim row() As String = {"Data 1", "Data 2", "Data 3"}
      DGV1.Rows.Add(row)

      Works perfectly. If you don't create the columns first, it'll bomb out with an InvalidOperationException - "No row can be added to a DataGridView control that does not have columns. Columns must be added first." I'm using .NET 2.0... There's got to be something else about the DGV setup or about the data that we haven't seen.

      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      D 2 Replies Last reply
      0
      • D Dave Kreskowiak

        Using the code you've supplied, I can't duplicate the problem. I actually can't BREAK it in any way unless I fail to define the columns in the DGV before I add the rows.

        With DGV1.Columns
        .Clear()
        .Add("Col1", "Column Header 1")
        .Add("Col2", "Column Header 2")
        .Add("Col3", "Column Header 3")
        End With

        Dim row() As String = {"Data 1", "Data 2", "Data 3"}
        DGV1.Rows.Add(row)

        Works perfectly. If you don't create the columns first, it'll bomb out with an InvalidOperationException - "No row can be added to a DataGridView control that does not have columns. Columns must be added first." I'm using .NET 2.0... There's got to be something else about the DGV setup or about the data that we haven't seen.

        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        D Offline
        D Offline
        DA_Loring
        wrote on last edited by
        #3

        Hi Dave, I don't have a problem with the column headers, but just so you can see, the columns are created thus: dgvData.Columns.Clear() dStartDay = mv_dCurrentAnomDate.AddDays(-2) iStartDayNumber = Weekday(dStartDay) dgvData.Columns.Add("Unit", "UNIT") For iLoopVar = 1 To 15 dgvData.Columns.Add("D" & Format(iLoopVar, "00"), Format(dStartDay, "dd-mm-yyyy") & Chr(10) & Format(dStartDay, "ddd")) dgvData.Columns(iLoopVar).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter dStartDay = dStartDay.AddDays(1) Next dgvData.ColumnHeadersHeight = 45 THe only error message I get is this "Index and length must refer to a location within the string. Parameter name: length" Source: mscorlib To be honest I don't actually understand what the error message is trying to tell me!

        David Loring !! Keep Music Live !!

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          Using the code you've supplied, I can't duplicate the problem. I actually can't BREAK it in any way unless I fail to define the columns in the DGV before I add the rows.

          With DGV1.Columns
          .Clear()
          .Add("Col1", "Column Header 1")
          .Add("Col2", "Column Header 2")
          .Add("Col3", "Column Header 3")
          End With

          Dim row() As String = {"Data 1", "Data 2", "Data 3"}
          DGV1.Rows.Add(row)

          Works perfectly. If you don't create the columns first, it'll bomb out with an InvalidOperationException - "No row can be added to a DataGridView control that does not have columns. Columns must be added first." I'm using .NET 2.0... There's got to be something else about the DGV setup or about the data that we haven't seen.

          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          D Offline
          D Offline
          DA_Loring
          wrote on last edited by
          #4

          Hi Dave, My face is seriously and completely covered in egg!! I apologise for wasting your time as it transpires that the error is actually occuring in the Cell_Enter event and I never saw this because I was stepping over the .add(row) A sad case of not being able to see the wood for the trees!:doh: Too many late nights, cigarettes and much too much coffee. Thanks again and apologies again.

          David Loring !! Keep Music Live !!

          D 1 Reply Last reply
          0
          • D DA_Loring

            Hi Dave, My face is seriously and completely covered in egg!! I apologise for wasting your time as it transpires that the error is actually occuring in the Cell_Enter event and I never saw this because I was stepping over the .add(row) A sad case of not being able to see the wood for the trees!:doh: Too many late nights, cigarettes and much too much coffee. Thanks again and apologies again.

            David Loring !! Keep Music Live !!

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            No problem! It happens to the best of us! :-D

            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups