I have a drop down box with a list of tables from a SQL CE database running on Windows Mobile 5, and once a user selects a table, all the data is bound to a datagrid. Now I'm trying to format the datagrid using a datagridtablest yle. It works for the first table, but if a user selects another table, it errors out. As a test I have the two functions CreateDataTable which creates a table with data and SetupTableStyle s which sets up the datagridtablest yle. The button calls CreateDataTable and then SetupTableStyle s. On the second press it throws an error of "Value does not fall within the expected range" when it tries to add the first custom column in SetupTableStyle s. Any idea on why this would occur?
Private Sub SetupTableStyle s(ByVal data As DataTable)
Dim alternatingColo r As Color = SystemColors.Co ntrolDark
Dim table As New DataTable()
table = data
Dim dataGridCustomC olumn0 As New DataGridCustomT extBoxColumn
With dataGridCustomC olumn0
.Owner = Me.DataGrid1
.Format = "0##"
.FormatInfo = Nothing
.HeaderText = table.Columns(0 ).ColumnName
.MappingName = table.Columns(0 ).ColumnName
.Width = DataGrid1.Width * 10 / 100
.AlternatingBac kColor = alternatingColo r
.ReadOnly = True
End With
DataGridTableSt yle1.GridColumn Styles.Add(data GridCustomColum n0)
Dim dataGridCustomC olumn1 As New DataGridCustomT extBoxColumn()
With dataGridCustomC olumn1
.Owner = Me.DataGrid1
.HeaderText = table.Columns(1 ).ColumnName
.MappingName = table.Columns(1 ).ColumnName
.NullText = ""
.Width = Me.DataGrid1.Wi dth * 40 / 100 ' 40%
.Alignment = HorizontalAlign ment.Right
.AlternatingBac kColor = alternatingColo r
End With
Me.DataGridTabl eStyle1.GridCol umnStyles.Add(d ataGridCustomCo lumn1)
Me.DataGridTabl eStyle1.Mapping Name = "Route"
Me.DataGrid1.Da taSource = table
End Sub
Private Sub CreateDataTable (ByVal num As Integer)
Dim Route As New DataTable("Rout e")
Route.Columns.A dd("Folio", GetType(String) )
Route.Columns.A dd("Address", GetType(String) )
For i As Integer = 0 To num
Route.Rows.Add( "test", "test1")
Next
SetupTableStyle s(Route)
End Sub
Private Sub SetupTableStyle s(ByVal data As DataTable)
Dim alternatingColo r As Color = SystemColors.Co ntrolDark
Dim table As New DataTable()
table = data
Dim dataGridCustomC olumn0 As New DataGridCustomT extBoxColumn
With dataGridCustomC olumn0
.Owner = Me.DataGrid1
.Format = "0##"
.FormatInfo = Nothing
.HeaderText = table.Columns(0 ).ColumnName
.MappingName = table.Columns(0 ).ColumnName
.Width = DataGrid1.Width * 10 / 100
.AlternatingBac kColor = alternatingColo r
.ReadOnly = True
End With
DataGridTableSt yle1.GridColumn Styles.Add(data GridCustomColum n0)
Dim dataGridCustomC olumn1 As New DataGridCustomT extBoxColumn()
With dataGridCustomC olumn1
.Owner = Me.DataGrid1
.HeaderText = table.Columns(1 ).ColumnName
.MappingName = table.Columns(1 ).ColumnName
.NullText = ""
.Width = Me.DataGrid1.Wi dth * 40 / 100 ' 40%
.Alignment = HorizontalAlign ment.Right
.AlternatingBac kColor = alternatingColo r
End With
Me.DataGridTabl eStyle1.GridCol umnStyles.Add(d ataGridCustomCo lumn1)
Me.DataGridTabl eStyle1.Mapping Name = "Route"
Me.DataGrid1.Da taSource = table
End Sub
Private Sub CreateDataTable (ByVal num As Integer)
Dim Route As New DataTable("Rout e")
Route.Columns.A dd("Folio", GetType(String) )
Route.Columns.A dd("Address", GetType(String) )
For i As Integer = 0 To num
Route.Rows.Add( "test", "test1")
Next
SetupTableStyle s(Route)
End Sub