A problem with an array populating an unbound DGV
-
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 !!
-
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 !!
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 WithDim 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 -
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 WithDim 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, 2007Hi 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 !!
-
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 WithDim 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, 2007Hi 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 !!
-
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 !!
No problem! It happens to the best of us! :-D
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007