PC Review


Reply
Thread Tools Rate Thread

Adding to a Datatable. Error: Index was outside the bounds of the array

 
 
Gerry Viator
Guest
Posts: n/a
 
      10th Jun 2004
Hello all,

I keep getting this error:

A first chance exception of type 'System.IndexOutOfRangeException' occurred
in system.windows.forms.dll
Additional information: Index was outside the bounds of the array.
At this line in the sub below: ShownDataTable.Rows.Add(NewRow)
I made the line bold below

I'll try and explain and enclude code.

I have a listview (MainListView)

And in the SelectedIndexChanged event I call the following sub, I also
declare in a main module a datatable(ShownDataTable)

Friend ShownDataTable As New DataTable("ShownValues")

*******************************************

Private Sub ShownItems()

ShownDataTable.Rows.Clear()
ShownDataTable.Columns.Clear()

Dim HowManyColumnsPartOf As Integer = sHowManyColumnsPartOf
Dim ColumnLocation As Integer = sColumnLocation
Dim ColumCount As Integer = ShownDataTable.Columns.Count
Dim RowsCount As Integer = ShownDataTable.Rows.Count

Dim cols As DataColumnCollection
Dim iCol As Integer
cols = MainSavedDataTable.Columns
If cols.Contains(sEntryName) Then
iCol = (cols.IndexOf(sEntryName))
End If


Dim CntColumn As New DataColumn("Entry #")
CntColumn.DataType = GetType(Integer)
CntColumn.AutoIncrement = True
CntColumn.AutoIncrementSeed = 1
ShownDataTable.Columns.Add(CntColumn)

Dim CntColumn2 As New DataColumn("Entry Name")
CntColumn2.DataType = GetType(String)
ShownDataTable.Columns.Add(CntColumn2)

Dim CntColumn3 As New DataColumn("Value")
CntColumn3.DataType = GetType(String)
ShownDataTable.Columns.Add(CntColumn3)

Entrygrid.DataSource = ShownDataTable

Dim NewColumn As DataColumn
For Each NewColumn In ShownDataTable.Columns
If NewColumn.ColumnName = "Entry #" Then
'Do nothing
ElseIf NewColumn.ColumnName = "Entry Name" Then

Dim Cnt As Integer
For Cnt = 1 To HowManyColumnsPartOf 'How many rows (Columns
from other datatable)

Dim Location As Integer
If ColumnLocation = 0 Then
ColumnLocation = 1
End If
If ColumnLocation < Cnt Then
Location = (Cnt - ColumnLocation) + iCol
ElseIf ColumnLocation = Cnt Then
Location = iCol
ElseIf ColumnLocation > Cnt Then
Location = iCol - (ColumnLocation - Cnt)
End If
Dim Strg As String =
MainSavedDataTable.Columns(Location).Caption & " -->"

Dim NewRow As DataRow
NewRow = ShownDataTable.NewRow
NewRow(NewColumn) = Strg
ShownDataTable.Rows.Add(NewRow)

Next

ElseIf NewColumn.ColumnName = "Value" Then

Dim Cnt As Integer
For Cnt = 1 To HowManyColumnsPartOf 'How many rows (Columns
from other datatable)
Dim Strg As String = ""
ShownDataTable.Rows(Cnt - 1).Item(NewColumn) = strg
Next
End If

Next


End Sub
*****************************************************
After the above sub I call another sub in the SelectedIndexChanged event

Private Sub AjustGrid()


Dim Graphics As Graphics = Entrygrid.CreateGraphics()
Dim TableStyle As DataGridTableStyle = New DataGridTableStyle

'********** to Adjust the Width of the Column

Dim NumberOfRowsToScan As Integer = ShownDataTable.Rows.Count

Entrygrid.TableStyles.Clear()
TableStyle.MappingName = ShownDataTable.TableName
TableStyle.HeaderBackColor = System.Drawing.Color.LightSteelBlue
TableStyle.BackColor = System.Drawing.Color.GhostWhite
TableStyle.AlternatingBackColor = System.Drawing.Color.GhostWhite
TableStyle.HeaderForeColor = System.Drawing.Color.DimGray
TableStyle.HeaderFont = New System.Drawing.Font("Tahoma", 8.0!,
System.Drawing.FontStyle.Bold, _
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
TableStyle.RowHeaderWidth = 0
TableStyle.SelectionBackColor = System.Drawing.Color.LightYellow
TableStyle.SelectionForeColor = System.Drawing.Color.Black


Dim TotalWidth As Integer = Nothing
Dim Column As DataColumn
Dim ColumnStyle As DataGridTextBoxColumn
Dim Width As Integer
Dim MaxPixelWidth As Integer = 300
For Each Column In ShownDataTable.Columns
ColumnStyle = New DataGridTextBoxColumn
With ColumnStyle
.TextBox.Enabled = True
.HeaderText = Column.ColumnName
.MappingName = Column.ColumnName
'Set width to header text width.
Width = Graphics.MeasureString(.HeaderText, Entrygrid.Font,
MaxPixelWidth).Width + 8
End With
'Change width, if data width is wider than header text width.
'Check the width of the data in the first X rows.
Dim iRow As Integer
Dim DataRow As DataRow
For iRow = 0 To NumberOfRowsToScan - 1
DataRow = ShownDataTable.Rows(iRow)
If Not IsDBNull(DataRow(Column.ColumnName)) Then
Width = System.Math.Max(Width,
Graphics.MeasureString(DataRow(Column.ColumnName), _
Entrygrid.Font, MaxPixelWidth).Width)
End If
Next
TotalWidth += Width
ColumnStyle.Width = Width + 4

'Add the new column style to the table style.
TableStyle.GridColumnStyles.Add(ColumnStyle)

Next


'Add the new table style to the data grid.
Entrygrid.TableStyles.Add(TableStyle)
Graphics.Dispose()

'*********
TotalWidth = TotalWidth + 36
Entrygrid.DataSource = ShownDataTable
Entrygrid.Size = New System.Drawing.Size(TotalWidth, 104)

End Sub


Thanks for your help
Gerry

 
Reply With Quote
 
 
 
 
chanmmn
Guest
Posts: n/a
 
      10th Jun 2004
My dear friend,

NewRow is keyword. Use other variable name.

chanmm
"Gerry Viator" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
Hello all,

I keep getting this error:

A first chance exception of type 'System.IndexOutOfRangeException' occurred in system.windows.forms.dll
Additional information: Index was outside the bounds of the array.
At this line in the sub below: ShownDataTable.Rows.Add(NewRow)
I made the line bold below

I'll try and explain and enclude code.

I have a listview (MainListView)

And in the SelectedIndexChanged event I call the following sub, I also
declare in a main module a datatable(ShownDataTable)

Friend ShownDataTable As New DataTable("ShownValues")

*******************************************

Private Sub ShownItems()

ShownDataTable.Rows.Clear()
ShownDataTable.Columns.Clear()

Dim HowManyColumnsPartOf As Integer = sHowManyColumnsPartOf
Dim ColumnLocation As Integer = sColumnLocation
Dim ColumCount As Integer = ShownDataTable.Columns.Count
Dim RowsCount As Integer = ShownDataTable.Rows.Count

Dim cols As DataColumnCollection
Dim iCol As Integer
cols = MainSavedDataTable.Columns
If cols.Contains(sEntryName) Then
iCol = (cols.IndexOf(sEntryName))
End If


Dim CntColumn As New DataColumn("Entry #")
CntColumn.DataType = GetType(Integer)
CntColumn.AutoIncrement = True
CntColumn.AutoIncrementSeed = 1
ShownDataTable.Columns.Add(CntColumn)

Dim CntColumn2 As New DataColumn("Entry Name")
CntColumn2.DataType = GetType(String)
ShownDataTable.Columns.Add(CntColumn2)

Dim CntColumn3 As New DataColumn("Value")
CntColumn3.DataType = GetType(String)
ShownDataTable.Columns.Add(CntColumn3)

Entrygrid.DataSource = ShownDataTable

Dim NewColumn As DataColumn
For Each NewColumn In ShownDataTable.Columns
If NewColumn.ColumnName = "Entry #" Then
'Do nothing
ElseIf NewColumn.ColumnName = "Entry Name" Then

Dim Cnt As Integer
For Cnt = 1 To HowManyColumnsPartOf 'How many rows (Columns from other datatable)

Dim Location As Integer
If ColumnLocation = 0 Then
ColumnLocation = 1
End If
If ColumnLocation < Cnt Then
Location = (Cnt - ColumnLocation) + iCol
ElseIf ColumnLocation = Cnt Then
Location = iCol
ElseIf ColumnLocation > Cnt Then
Location = iCol - (ColumnLocation - Cnt)
End If
Dim Strg As String = MainSavedDataTable.Columns(Location).Caption & " -->"

Dim NewRow As DataRow
NewRow = ShownDataTable.NewRow
NewRow(NewColumn) = Strg
ShownDataTable.Rows.Add(NewRow)

Next

ElseIf NewColumn.ColumnName = "Value" Then

Dim Cnt As Integer
For Cnt = 1 To HowManyColumnsPartOf 'How many rows (Columns from other datatable)
Dim Strg As String = ""
ShownDataTable.Rows(Cnt - 1).Item(NewColumn) = strg
Next
End If

Next


End Sub
*****************************************************
After the above sub I call another sub in the SelectedIndexChanged event

Private Sub AjustGrid()


Dim Graphics As Graphics = Entrygrid.CreateGraphics()
Dim TableStyle As DataGridTableStyle = New DataGridTableStyle

'********** to Adjust the Width of the Column

Dim NumberOfRowsToScan As Integer = ShownDataTable.Rows.Count

Entrygrid.TableStyles.Clear()
TableStyle.MappingName = ShownDataTable.TableName
TableStyle.HeaderBackColor = System.Drawing.Color.LightSteelBlue
TableStyle.BackColor = System.Drawing.Color.GhostWhite
TableStyle.AlternatingBackColor = System.Drawing.Color.GhostWhite
TableStyle.HeaderForeColor = System.Drawing.Color.DimGray
TableStyle.HeaderFont = New System.Drawing.Font("Tahoma", 8.0!, System.Drawing.FontStyle.Bold, _
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
TableStyle.RowHeaderWidth = 0
TableStyle.SelectionBackColor = System.Drawing.Color.LightYellow
TableStyle.SelectionForeColor = System.Drawing.Color.Black


Dim TotalWidth As Integer = Nothing
Dim Column As DataColumn
Dim ColumnStyle As DataGridTextBoxColumn
Dim Width As Integer
Dim MaxPixelWidth As Integer = 300
For Each Column In ShownDataTable.Columns
ColumnStyle = New DataGridTextBoxColumn
With ColumnStyle
.TextBox.Enabled = True
.HeaderText = Column.ColumnName
.MappingName = Column.ColumnName
'Set width to header text width.
Width = Graphics.MeasureString(.HeaderText, Entrygrid.Font, MaxPixelWidth).Width + 8
End With
'Change width, if data width is wider than header text width.
'Check the width of the data in the first X rows.
Dim iRow As Integer
Dim DataRow As DataRow
For iRow = 0 To NumberOfRowsToScan - 1
DataRow = ShownDataTable.Rows(iRow)
If Not IsDBNull(DataRow(Column.ColumnName)) Then
Width = System.Math.Max(Width, Graphics.MeasureString(DataRow(Column.ColumnName), _
Entrygrid.Font, MaxPixelWidth).Width)
End If
Next
TotalWidth += Width
ColumnStyle.Width = Width + 4

'Add the new column style to the table style.
TableStyle.GridColumnStyles.Add(ColumnStyle)

Next


'Add the new table style to the data grid.
Entrygrid.TableStyles.Add(TableStyle)
Graphics.Dispose()

'*********
TotalWidth = TotalWidth + 36
Entrygrid.DataSource = ShownDataTable
Entrygrid.Size = New System.Drawing.Size(TotalWidth, 104)

End Sub


Thanks for your help
Gerry
 
Reply With Quote
 
Gerry Viator
Guest
Posts: n/a
 
      10th Jun 2004
Thanks, I changed that

Still the same problem

thanks
Gerry
"chanmmn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
My dear friend,

NewRow is keyword. Use other variable name.

chanmm
"Gerry Viator" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
Hello all,

I keep getting this error:

A first chance exception of type 'System.IndexOutOfRangeException'
occurred in system.windows.forms.dll
Additional information: Index was outside the bounds of the
array.
At this line in the sub below: ShownDataTable.Rows.Add(NewRow)
I made the line bold below

I'll try and explain and enclude code.

I have a listview (MainListView)

And in the SelectedIndexChanged event I call the following sub, I also
declare in a main module a datatable(ShownDataTable)

Friend ShownDataTable As New DataTable("ShownValues")

*******************************************

Private Sub ShownItems()

ShownDataTable.Rows.Clear()
ShownDataTable.Columns.Clear()

Dim HowManyColumnsPartOf As Integer = sHowManyColumnsPartOf
Dim ColumnLocation As Integer = sColumnLocation
Dim ColumCount As Integer = ShownDataTable.Columns.Count
Dim RowsCount As Integer = ShownDataTable.Rows.Count

Dim cols As DataColumnCollection
Dim iCol As Integer
cols = MainSavedDataTable.Columns
If cols.Contains(sEntryName) Then
iCol = (cols.IndexOf(sEntryName))
End If


Dim CntColumn As New DataColumn("Entry #")
CntColumn.DataType = GetType(Integer)
CntColumn.AutoIncrement = True
CntColumn.AutoIncrementSeed = 1
ShownDataTable.Columns.Add(CntColumn)

Dim CntColumn2 As New DataColumn("Entry Name")
CntColumn2.DataType = GetType(String)
ShownDataTable.Columns.Add(CntColumn2)

Dim CntColumn3 As New DataColumn("Value")
CntColumn3.DataType = GetType(String)
ShownDataTable.Columns.Add(CntColumn3)

Entrygrid.DataSource = ShownDataTable

Dim NewColumn As DataColumn
For Each NewColumn In ShownDataTable.Columns
If NewColumn.ColumnName = "Entry #" Then
'Do nothing
ElseIf NewColumn.ColumnName = "Entry Name" Then

Dim Cnt As Integer
For Cnt = 1 To HowManyColumnsPartOf 'How many rows
(Columns from other datatable)

Dim Location As Integer
If ColumnLocation = 0 Then
ColumnLocation = 1
End If
If ColumnLocation < Cnt Then
Location = (Cnt - ColumnLocation) + iCol
ElseIf ColumnLocation = Cnt Then
Location = iCol
ElseIf ColumnLocation > Cnt Then
Location = iCol - (ColumnLocation - Cnt)
End If
Dim Strg As String =
MainSavedDataTable.Columns(Location).Caption & " -->"

Dim NewRow As DataRow
NewRow = ShownDataTable.NewRow
NewRow(NewColumn) = Strg
ShownDataTable.Rows.Add(NewRow)

Next

ElseIf NewColumn.ColumnName = "Value" Then

Dim Cnt As Integer
For Cnt = 1 To HowManyColumnsPartOf 'How many rows
(Columns from other datatable)
Dim Strg As String = ""
ShownDataTable.Rows(Cnt - 1).Item(NewColumn) = strg
Next
End If

Next


End Sub
*****************************************************
After the above sub I call another sub in the SelectedIndexChanged event

Private Sub AjustGrid()


Dim Graphics As Graphics = Entrygrid.CreateGraphics()
Dim TableStyle As DataGridTableStyle = New DataGridTableStyle

'********** to Adjust the Width of the Column

Dim NumberOfRowsToScan As Integer = ShownDataTable.Rows.Count

Entrygrid.TableStyles.Clear()
TableStyle.MappingName = ShownDataTable.TableName
TableStyle.HeaderBackColor = System.Drawing.Color.LightSteelBlue
TableStyle.BackColor = System.Drawing.Color.GhostWhite
TableStyle.AlternatingBackColor =
System.Drawing.Color.GhostWhite
TableStyle.HeaderForeColor = System.Drawing.Color.DimGray
TableStyle.HeaderFont = New System.Drawing.Font("Tahoma", 8.0!,
System.Drawing.FontStyle.Bold, _
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
TableStyle.RowHeaderWidth = 0
TableStyle.SelectionBackColor = System.Drawing.Color.LightYellow
TableStyle.SelectionForeColor = System.Drawing.Color.Black


Dim TotalWidth As Integer = Nothing
Dim Column As DataColumn
Dim ColumnStyle As DataGridTextBoxColumn
Dim Width As Integer
Dim MaxPixelWidth As Integer = 300
For Each Column In ShownDataTable.Columns
ColumnStyle = New DataGridTextBoxColumn
With ColumnStyle
.TextBox.Enabled = True
.HeaderText = Column.ColumnName
.MappingName = Column.ColumnName
'Set width to header text width.
Width = Graphics.MeasureString(.HeaderText,
Entrygrid.Font, MaxPixelWidth).Width + 8
End With
'Change width, if data width is wider than header text
width.
'Check the width of the data in the first X rows.
Dim iRow As Integer
Dim DataRow As DataRow
For iRow = 0 To NumberOfRowsToScan - 1
DataRow = ShownDataTable.Rows(iRow)
If Not IsDBNull(DataRow(Column.ColumnName)) Then
Width = System.Math.Max(Width,
Graphics.MeasureString(DataRow(Column.ColumnName), _
Entrygrid.Font, MaxPixelWidth).Width)
End If
Next
TotalWidth += Width
ColumnStyle.Width = Width + 4

'Add the new column style to the table style.
TableStyle.GridColumnStyles.Add(ColumnStyle)

Next


'Add the new table style to the data grid.
Entrygrid.TableStyles.Add(TableStyle)
Graphics.Dispose()

'*********
TotalWidth = TotalWidth + 36
Entrygrid.DataSource = ShownDataTable
Entrygrid.Size = New System.Drawing.Size(TotalWidth, 104)

End Sub


Thanks for your help
Gerry

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index array out of bounds - error message =?Utf-8?B?VGFyYQ==?= Microsoft Outlook Discussion 0 26th Oct 2007 06:25 PM
Error: Index is outside the bounds of the array weird0 Microsoft C# .NET 4 4th Apr 2007 10:37 PM
Error [Index was outside the bounds of the array] ?! Agnes Microsoft VB .NET 3 4th Jul 2004 03:36 PM
Error: "Index was outside the bounds of the array" when binding the datatable with datagrid Gerry Viator Microsoft ADO .NET 1 11th Jun 2004 06:04 PM
Error Message - Array Index Out of Bounds =?Utf-8?B?Uks=?= Microsoft Outlook Contacts 1 10th Feb 2004 12:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:52 AM.