473,659 Members | 3,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array of Datagrids troubles

(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.Dat aSet
Dim OleDbConnection 1(20) As System.Data.Ole Db.OleDbConnect ion
Dim OleDbDataAdapte r1(20) As System.Data.Ole Db.OleDbDataAda pter
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 PopulateGridWit hXL(ByVal FileSpec As String, ByVal sheet As
String)

OleDbConnection 1(CurrentIndex) = New System.Data.Ole Db.OleDbConnect ion
("provider=Micr osoft.Jet.OLEDB .4.0; " & "data source=" & FileSpec & ";" & _
"Extended Properties=Exce l 8.0;")
OleDbDataAdapte r1(CurrentIndex ) = New System.Data.Ole Db.OleDbDataAda pter(
"select * from [" & sheet & "]", OleDbConnection 1(CurrentIndex) )

datagrid1 = New DataGrid
Controls.Add(da tagrid1)
Try
DS = New System.Data.Dat aSet
OleDbDataAdapte r1(CurrentIndex ).Fill(DS)
DataGrid1.DataS ource = DS.Tables(0)
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try
OleDbConnection 1(CurrentIndex) .Close()
CurrentIndex = CurrentIndex + 1
DataGrid1.Visib le = 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(Curr entIndex)" Giving me:

Private Sub PopulateGridWit hXL(ByVal FileSpec As String, ByVal sheet As
String)

OleDbConnection 1(CurrentIndex) = New System.Data.Ole Db.OleDbConnect ion
("provider=Micr osoft.Jet.OLEDB .4.0; " & "data source=" & FileSpec & ";" & _
"Extended Properties=Exce l 8.0;")
OleDbDataAdapte r1(CurrentIndex ) = New System.Data.Ole Db.OleDbDataAda pter(
"select * from [" & sheet & "]", OleDbConnection 1(CurrentIndex) )
datagrid2(Curre ntIndex) = New DataGrid
Controls.Add(da tagrid2(Current Index))
Try
DS = New System.Data.Dat aSet
OleDbDataAdapte r1(CurrentIndex ).Fill(DS)
datagrid2(Curre ntIndex).DataSo urce = DS.Tables(0)
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try
OleDbConnection 1(CurrentIndex) .Close()
CurrentIndex = CurrentIndex + 1
datagrid2(Curre ntIndex).Visibl e = 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(Curr entIndex).Visib le = 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?

--
mark b
Jan 1 '06 #1
4 1171
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" <ma**@discussio ns.microsoft.co m> wrote in message
news:9A******** *************** ***********@mic rosoft.com...
(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.Dat aSet
Dim OleDbConnection 1(20) As System.Data.Ole Db.OleDbConnect ion
Dim OleDbDataAdapte r1(20) As
System.Data.Ole Db.OleDbDataAda pter
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 PopulateGridWit hXL(ByVal FileSpec As String, ByVal sheet As
String)

OleDbConnection 1(CurrentIndex) = New System.Data.Ole Db.OleDbConnect ion
("provider=Micr osoft.Jet.OLEDB .4.0; " & "data source=" & FileSpec & ";" &
_
"Extended Properties=Exce l 8.0;")
OleDbDataAdapte r1(CurrentIndex ) = New System.Data.Ole Db.OleDbDataAda pter(
"select * from [" & sheet & "]", OleDbConnection 1(CurrentIndex) )

datagrid1 = New DataGrid
Controls.Add(da tagrid1)
Try
DS = New System.Data.Dat aSet
OleDbDataAdapte r1(CurrentIndex ).Fill(DS)
DataGrid1.DataS ource = DS.Tables(0)
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try
OleDbConnection 1(CurrentIndex) .Close()
CurrentIndex = CurrentIndex + 1
DataGrid1.Visib le = 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(Curr entIndex)" Giving
me:

Private Sub PopulateGridWit hXL(ByVal FileSpec As String, ByVal sheet As
String)

OleDbConnection 1(CurrentIndex) = New System.Data.Ole Db.OleDbConnect ion
("provider=Micr osoft.Jet.OLEDB .4.0; " & "data source=" & FileSpec & ";" &
_
"Extended Properties=Exce l 8.0;")
OleDbDataAdapte r1(CurrentIndex ) = New System.Data.Ole Db.OleDbDataAda pter(
"select * from [" & sheet & "]", OleDbConnection 1(CurrentIndex) )
datagrid2(Curre ntIndex) = New DataGrid
Controls.Add(da tagrid2(Current Index))
Try
DS = New System.Data.Dat aSet
OleDbDataAdapte r1(CurrentIndex ).Fill(DS)
datagrid2(Curre ntIndex).DataSo urce = DS.Tables(0)
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try
OleDbConnection 1(CurrentIndex) .Close()
CurrentIndex = CurrentIndex + 1
datagrid2(Curre ntIndex).Visibl e = 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(Curr entIndex).Visib le = 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?

--
mark b

Jan 1 '06 #2

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

--
mark b
"Ken Tucker [MVP]" wrote:
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" <ma**@discussio ns.microsoft.co m> wrote in message
news:9A******** *************** ***********@mic rosoft.com...
(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.Dat aSet
Dim OleDbConnection 1(20) As System.Data.Ole Db.OleDbConnect ion
Dim OleDbDataAdapte r1(20) As
System.Data.Ole Db.OleDbDataAda pter
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 PopulateGridWit hXL(ByVal FileSpec As String, ByVal sheet As
String)

OleDbConnection 1(CurrentIndex) = New System.Data.Ole Db.OleDbConnect ion
("provider=Micr osoft.Jet.OLEDB .4.0; " & "data source=" & FileSpec & ";" &
_
"Extended Properties=Exce l 8.0;")
OleDbDataAdapte r1(CurrentIndex ) = New System.Data.Ole Db.OleDbDataAda pter(
"select * from [" & sheet & "]", OleDbConnection 1(CurrentIndex) )

datagrid1 = New DataGrid
Controls.Add(da tagrid1)
Try
DS = New System.Data.Dat aSet
OleDbDataAdapte r1(CurrentIndex ).Fill(DS)
DataGrid1.DataS ource = DS.Tables(0)
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try
OleDbConnection 1(CurrentIndex) .Close()
CurrentIndex = CurrentIndex + 1
DataGrid1.Visib le = 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(Curr entIndex)" Giving
me:

Private Sub PopulateGridWit hXL(ByVal FileSpec As String, ByVal sheet As
String)

OleDbConnection 1(CurrentIndex) = New System.Data.Ole Db.OleDbConnect ion
("provider=Micr osoft.Jet.OLEDB .4.0; " & "data source=" & FileSpec & ";" &
_
"Extended Properties=Exce l 8.0;")
OleDbDataAdapte r1(CurrentIndex ) = New System.Data.Ole Db.OleDbDataAda pter(
"select * from [" & sheet & "]", OleDbConnection 1(CurrentIndex) )
datagrid2(Curre ntIndex) = New DataGrid
Controls.Add(da tagrid2(Current Index))
Try
DS = New System.Data.Dat aSet
OleDbDataAdapte r1(CurrentIndex ).Fill(DS)
datagrid2(Curre ntIndex).DataSo urce = DS.Tables(0)
Catch ex As Exception
MsgBox(ex.Messa ge)
End Try
OleDbConnection 1(CurrentIndex) .Close()
CurrentIndex = CurrentIndex + 1
datagrid2(Curre ntIndex).Visibl e = 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(Curr entIndex).Visib le = 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?

--
mark b


Jan 1 '06 #3
Mark,

For this problem it is in my opinin not important what control it is.

Have a look at this sample.

http://www.vb-tips.com/default.aspx?...2-03abce36aa60

I hope this helps,

Cor
Jan 1 '06 #4
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 )
--
mark b
"Cor Ligthert [MVP]" wrote:
Mark,

For this problem it is in my opinin not important what control it is.

Have a look at this sample.

http://www.vb-tips.com/default.aspx?...2-03abce36aa60

I hope this helps,

Cor

Jan 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1544
by: Tom Page | last post by:
Hello all I have a question that may or may not be simple - I need my three dimensional array to be of the form: array where jj and kk range from 0 to some constant fixed at the very beginnning of the program (currently 20) but goes from 0 to some boundary that depends on calculations performed within the program. I know it is possible
4
5026
by: Haydnw | last post by:
Hi, I'd like to put a load of database results (several rows for 5 fields) into a two-dimensional array. Now, this may be a really stupid question, but can someone give me a pointer for how to do it? I can bind data to datagrids and lists and stuff all day long, but can't seem to grasp this one. Any pointers to a useful article / demo (or just any useful pointers!) would be much appreciated. Thanks,
4
416
by: ree32 | last post by:
I have a placeholder and depending on a user input(a drop downlist) when the user clicks a button I dynamically create a number of datagrids and fill them with data from a database. But the problem is that on a postback I lose all the datagrids and their data. I have looked at numerous pages on the net regarding this issue. Many say you have to rebuild build the controls on postback. How am I meant to do this in my situation as I don't...
10
2747
by: mark | last post by:
I have a simple windows application that has a function to read a csv file and enter the values into an array A as double(,). Also, an instance of form 2 (which has a DataGrid) is created and the values of A are used to populate the grid via a DataView. The instance of form2 is called Matrix_A . The basic layout is as follows: #Region "Globals Dim A As Double(,) Dim Matrix_A As form2 #End Region
8
1855
by: zulander | last post by:
is there a way to find out if the element exist ? dim myarray() as string dim mytxt as string mytxt ="Superman1,Superman2,Superman3,Superman4" myarray=mytxt.split(",") debug.print(myarray(4))
5
2391
by: xxx | last post by:
Hi all, i'm new in visual c++ and i'm having troubles converting types. Let me explain: i have an unmanaged c++ function that wants an float* parameter but i have an array<float>^, how i can covert it? the following code doesn't show up any error during compile time but crashes at runtime telling "unrecognized or unsupported array type": array<float>^ vals = gcnew array<float>(dimension); pin_ptr<float>ss=&vals; unmanaged_function(ss);
7
1842
by: Ausclad | last post by:
Ok, ill try again..... It seems fairly simple. I have two combo boxes in a datagrid. The datagrid is bound to a a table in a dataset. The two combo boxes are bound to a single data table in a different dataset. One combo box is displaying the Employees payroll ID. The other is
0
864
by: hermiod | last post by:
Hi all This is my first post. I`ve looked back at the previous array discussions and haven`t found an answer to my problem so apologies if I am re-asking something that has already been asked in the past. I have the following function in a .net class: Public Shared Function ReturnAll(ByVal odfpath As String) As String() Dim ReturnArray(1) As String Dim returntext As String = "" Dim c() As String = Nothing ...
2
1929
by: rn5a | last post by:
In a shopping cart app, a ASPX page retrieves the order details & personal details of a user from a MS-Access database table depending upon the username of the user. The order details of a particular order (like ProductID, Name, Description, Quantity etc.) are displayed in one DataGrid where as the personal details of the buyer corresponding to this order (like Name, E-Mail, Shipping & Billing Address etc.) are displayed in another...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.