Array of Datagrids troubles

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?
 
K

Ken Tucker [MVP]

Hi,

Why dont you add a combobox to your form with a list of the
worksheets. When a user selects one of the work sheets load the data then
display it in the datagrid. I do not see a reason to have 20 datagrids.

Ken
--------------------
mark said:
(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?
 
G

Guest

Potentially need simultaneous view (and add row) of up to 20 grids.



--
mark b


Ken Tucker said:
Hi,

Why dont you add a combobox to your form with a list of the
worksheets. When a user selects one of the work sheets load the data then
display it in the datagrid. I do not see a reason to have 20 datagrids.

Ken
--------------------
mark said:
(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?
 
G

Guest

That works great. Great webpage too. I added it to my bookmarks.

My modification for grids looks like:

Dim DG As New DataGrid
DG.Location = New Drawing.Point(8, 8 + i * 80)
DG.TabIndex = i
DG.Text = i.ToString
DG.Tag = i.ToString
DG.Name = "DG" & i.ToString
AddHandler DG.Click, AddressOf Clickgrid
Controls.Add(DG)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top