use what collection object to hold existing datatable?

G

Guest

Hello,

In my vb2005 app I create 3 datatables in the Datasource area, tbl1, tbl2,
tbl3. tbl1 contains 1 column, tbl2 contains 2 columns tbl3 contains 3
columns.

Based on selections from a form, I want to populate on 1 of the 3 tables
with data and use the selected table throughout some procedure - the table
and the data that it contains will have to persist throughout the life of the
application. So instead of having a bunch of If-Then statements for each
table I want to hold the selected table (the actual table - not a copy of the
table) in some collection object. I guess dTbl() would be a reference to the
selected table (or a pointer to the selected table in C++)

Here is something I tried that is not working
Dim dTbl() as DataTable = New DataTable(){}

If strSelected.Equals("tbl1") then dTbl(0) = dataset.tbl1
If strSelected.Equals("tbl2") then dtbl(0) = dataset.tbl2
If strSelected.Equals("tbl3") then dTbl(0) = dataset.tbl3

So the idea is that dTbl(0) would be used throughout the app, but when
dTbl(0) gets populated, it is actually dataset.tbl1 (or 2 or 3) that is being
populated. What object can I use to do this?

Thanks,
Rich
 
A

AMDRIT

I am having a hard time understanding your question, please enlighten me.

if you are wanting to set a place holder for the selected table you could do
something like:

private mSelectedTable as datatable

sub SetSelected
If strSelected.Equals("tbl1") then mSelectedTable = dataset.tbl1
If strSelected.Equals("tbl2") then mSelectedTable = dataset.tbl2
If strSelected.Equals("tbl3") then mSelectedTable = dataset.tbl3
end Sub


if you are wanting to be able to refer to the table by selection then you
can do something like:

private mTables as HashTable

Sub SetSelected
if mTables is nothing then
mTables.add("tbl1", dataset.tbl1)
mTables.add("tbl2", dataset.tbl2)
mTables.add("tbl2", dataset.tbl2)
end if
End Sub

Sub DoWork
SetSelected

ctype(mtables(strSelected),datatable).rows(someindex).item(somecolumn) =
"somevalue"

end Sub
 
G

Guest

Thanks for your reply. What I ended up doing was this:

Private sub someProc(ByVal strSelected As String)
Dim dTbl() As DataTable = New DataTable(){ds.tbl1, ds.tbl2, ds.tbl3}
Dim iTbl As Integer
If strSelected.Equals("tbl1") Then iTbl = 0
If strSelected.Equals("tbl2") Then iTbl = 1
If strSelected.Equals("tbl3") Then iTbl = 2

Do stuff with dTbl(iTbl)
....
datagridview1.Datasource = dTbl(iTbl)

End Sub

My procedure will collect data from sql server and place it in the selected
table. Once the procedure has completed, the pull result will be displayed
in the datagridview. The user has the option of adding additional data to
the selected table(s) after the procedure has gone out of scope. tbl1, tbl2,
tbl3 are global tables in the app. The default option is to clear all the
tables after the procedure has completed. But the user can select to not
clear the table(s) after the procedure has completed. The procedure only
acts on one table at a time. So I needed a way to specify which table was
the currently selected table.
 
B

Branco Medeiros

Rich wrote:
Private sub someProc(ByVal strSelected As String)
Dim dTbl() As DataTable = New DataTable(){ds.tbl1, ds.tbl2, ds.tbl3}
Dim iTbl As Integer
If strSelected.Equals("tbl1") Then iTbl = 0
If strSelected.Equals("tbl2") Then iTbl = 1
If strSelected.Equals("tbl3") Then iTbl = 2

Do stuff with dTbl(iTbl)
...
datagridview1.Datasource = dTbl(iTbl)

End Sub
<snip>

What I don't understand -- I guess you have your reasons -- is why
didn't you simply assigned the table to a variable instead of using an
index into a collection.

Something like:

Private sub someProc(ByVal strSelected As String)
Dim TheTable As DataTable
Select Case strSelected.ToLower
Case "tbl1": TheTable = ds.Tbl1
Case "tbl2": TheTable = ds.Tbl2
Case "tbl3": TehTable = ds.Tbl3
Case Else
'Oops, throw error
End Select
'Do Stuff with TheTable
'...
datagridview1.DataSource = TheTable
'...
End Sub

Regards,

Branco.
 
G

Guest

I did try a derivation of that, but it did not populate the global table.
That trick only populates TheTable table object. I need to be able to
populate the global table cummulatively. I could transfer the data from
TheTable to the selected global table, but that would be a little redundant.

The trick I am using now of just placing the global tables in a Table array
is working. And I am sure there is a way to do my thing without having to
load all the global tables in a table array. I will probably stumble onto
that trick down the road.
 

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