G
Guest
(windows app not web)
I have a procedure to populate a given datagrid (Datagrid1) with a dataset
from an indexed data connection. It works:
Globals: Dim FileName1 As String
Dim DS As System.Data.DataSet
Dim OleDbConnection1(20) As System.Data.OleDb.OleDbConnection
Dim OleDbDataAdapter1(20) As System.Data.OleDb.OleDbDataAdapter
Dim CurrentIndex As Integer = 0
Friend WithEvents datagrid1 As DataGrid
A sub allows me to browse and fetch the (Excel) filename and sheet I want.
Then this sub fills and displays the grid
:
Private Sub PopulateGridWithXL(ByVal FileSpec As String, ByVal sheet As
String)
OleDbConnection1(CurrentIndex) = New System.Data.OleDb.OleDbConnection
("provider=Microsoft.Jet.OLEDB.4.0; " & "data source=" & FileSpec & ";" & _
"Extended Properties=Excel 8.0;")
OleDbDataAdapter1(CurrentIndex) = New System.Data.OleDb.OleDbDataAdapter(
"select * from [" & sheet & "]", OleDbConnection1(CurrentIndex))
datagrid1 = New DataGrid
Controls.Add(datagrid1)
Try
DS = New System.Data.DataSet
OleDbDataAdapter1(CurrentIndex).Fill(DS)
DataGrid1.DataSource = DS.Tables(0)
Catch ex As Exception
MsgBox(ex.Message)
End Try
OleDbConnection1(CurrentIndex).Close()
CurrentIndex = CurrentIndex + 1
DataGrid1.Visible = True
End Sub
Naturally, everytime I call the sub it overwrites Datagrid1 and I want a new
datagrid for each call. So, I believe I need an array of datagrids.
I added Dim datagrid2(20) As DataGrid to my globals. And replaced each
occurrence of "DataGrid1" in my sub with "datagrid2(CurrentIndex)" Giving me:
Private Sub PopulateGridWithXL(ByVal FileSpec As String, ByVal sheet As
String)
OleDbConnection1(CurrentIndex) = New System.Data.OleDb.OleDbConnection
("provider=Microsoft.Jet.OLEDB.4.0; " & "data source=" & FileSpec & ";" & _
"Extended Properties=Excel 8.0;")
OleDbDataAdapter1(CurrentIndex) = New System.Data.OleDb.OleDbDataAdapter(
"select * from [" & sheet & "]", OleDbConnection1(CurrentIndex))
datagrid2(CurrentIndex) = New DataGrid
Controls.Add(datagrid2(CurrentIndex))
Try
DS = New System.Data.DataSet
OleDbDataAdapter1(CurrentIndex).Fill(DS)
datagrid2(CurrentIndex).DataSource = DS.Tables(0)
Catch ex As Exception
MsgBox(ex.Message)
End Try
OleDbConnection1(CurrentIndex).Close()
CurrentIndex = CurrentIndex + 1
datagrid2(CurrentIndex).Visible = True
End Sub
When I execute the sub it fails, throwing the exception: Object reference
not set to an instance of an object on the last line
(datagrid2(CurrentIndex).Visible = True)
What is it that I am missing here? The logic works with the Connection and
the adaptor, but not the datagrid.
Is there a better practice for creating an indeterminate number of DGs?
Evidently, I cannot declare an array of datagrid with events. Is there a way
to make individual datagrid elements of such an array raise the usual set of
DG events?
I have a procedure to populate a given datagrid (Datagrid1) with a dataset
from an indexed data connection. It works:
Globals: Dim FileName1 As String
Dim DS As System.Data.DataSet
Dim OleDbConnection1(20) As System.Data.OleDb.OleDbConnection
Dim OleDbDataAdapter1(20) As System.Data.OleDb.OleDbDataAdapter
Dim CurrentIndex As Integer = 0
Friend WithEvents datagrid1 As DataGrid
A sub allows me to browse and fetch the (Excel) filename and sheet I want.
Then this sub fills and displays the grid
:
Private Sub PopulateGridWithXL(ByVal FileSpec As String, ByVal sheet As
String)
OleDbConnection1(CurrentIndex) = New System.Data.OleDb.OleDbConnection
("provider=Microsoft.Jet.OLEDB.4.0; " & "data source=" & FileSpec & ";" & _
"Extended Properties=Excel 8.0;")
OleDbDataAdapter1(CurrentIndex) = New System.Data.OleDb.OleDbDataAdapter(
"select * from [" & sheet & "]", OleDbConnection1(CurrentIndex))
datagrid1 = New DataGrid
Controls.Add(datagrid1)
Try
DS = New System.Data.DataSet
OleDbDataAdapter1(CurrentIndex).Fill(DS)
DataGrid1.DataSource = DS.Tables(0)
Catch ex As Exception
MsgBox(ex.Message)
End Try
OleDbConnection1(CurrentIndex).Close()
CurrentIndex = CurrentIndex + 1
DataGrid1.Visible = True
End Sub
Naturally, everytime I call the sub it overwrites Datagrid1 and I want a new
datagrid for each call. So, I believe I need an array of datagrids.
I added Dim datagrid2(20) As DataGrid to my globals. And replaced each
occurrence of "DataGrid1" in my sub with "datagrid2(CurrentIndex)" Giving me:
Private Sub PopulateGridWithXL(ByVal FileSpec As String, ByVal sheet As
String)
OleDbConnection1(CurrentIndex) = New System.Data.OleDb.OleDbConnection
("provider=Microsoft.Jet.OLEDB.4.0; " & "data source=" & FileSpec & ";" & _
"Extended Properties=Excel 8.0;")
OleDbDataAdapter1(CurrentIndex) = New System.Data.OleDb.OleDbDataAdapter(
"select * from [" & sheet & "]", OleDbConnection1(CurrentIndex))
datagrid2(CurrentIndex) = New DataGrid
Controls.Add(datagrid2(CurrentIndex))
Try
DS = New System.Data.DataSet
OleDbDataAdapter1(CurrentIndex).Fill(DS)
datagrid2(CurrentIndex).DataSource = DS.Tables(0)
Catch ex As Exception
MsgBox(ex.Message)
End Try
OleDbConnection1(CurrentIndex).Close()
CurrentIndex = CurrentIndex + 1
datagrid2(CurrentIndex).Visible = True
End Sub
When I execute the sub it fails, throwing the exception: Object reference
not set to an instance of an object on the last line
(datagrid2(CurrentIndex).Visible = True)
What is it that I am missing here? The logic works with the Connection and
the adaptor, but not the datagrid.
Is there a better practice for creating an indeterminate number of DGs?
Evidently, I cannot declare an array of datagrid with events. Is there a way
to make individual datagrid elements of such an array raise the usual set of
DG events?