G
Gerry Viator
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
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