Binding DataTable reference to a DataGrid

C

Cip

Hi,

If I have this:

Dim myTable as new DataTable
Dim grid as new DataGrid

myTable = MyObject.GetTable1()
grid.dataSource = myTable

myTable = MyObject.GetTable2()


How can I get the grid to bind itself to myTable so that after myTable
= MyObject.GetTable2(), the grid automatically repopulates itself with
the new data ?

I am basically asking if I can bind the grid to the actual myTable
variable rather than the DataTable object myTable is referencing.

If that is not possible then how can I make myTable =
MyObject.GetTable2() set the original DataTable object referenced by
myTable to MyObject.GetTable2().

I think that currently myTable = MyObject.GetTable2() creates a new
DataTable object and makes myTable point to this new DataTable,
without changing the original DataTable object.
 
W

William Ryan eMVP

Hi Cip:

Since datatables are reference types, there's really no difference (unless
your GetTable is doing something that points it to some other table).
What does GetTable do? It's hard to tell without seeing that, but try
refreshing the grid after you reset the reference.
 
C

Cor

Hi Cip,

I would not think to long about that.
See inline

Cor
Dim myTable as new DataTable
Dim grid as new DataGrid

myTable = MyObject.GetTable1() grid.dataSource = nothing
grid.dataSource = myTable
myTable = MyObject.GetTable2() grid.dataSource = nothing
grid.dataSource = myTable

Cor
 
C

Cip

William Ryan eMVP said:
Hi Cip:

Since datatables are reference types, there's really no difference (unless
your GetTable is doing something that points it to some other table).
What does GetTable do? It's hard to tell without seeing that, but try
refreshing the grid after you reset the reference.

After looking into it GetTable does something like this:

Public Function GetTable() as DataTable
Dim newTable as New DataTable
'Here it loops and adds rows...
return newTable
End Function


and we had:
myTable = myObject.GetTable()

I guess that by creating a new DataTable reference, the original
myTable object now points to this new reference.

The old DataTable object referenced by myTable (before the function
call) is still used by DataGrid.

Can I not change the actual object referenced by myTable rather than
making myTable point to a different DataTable object ?
 

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