GetProperties

G

Guest

I am trying to get the properties of a DataTable or DataView using the
following code where obj is an object set to either a DataTable or DataView:

If TypeOf obj Is DataSet Then
myDataList = CType(CType(obj, DataSet).Tables(v_DataMember), _
IListSource).GetList
ElseIf TypeOf obj Is IListSource Then
myDataList = CType(obj, IListSource).GetList
End If
Dim obj As Object = v_DataList.Item(0)
Dim props As PropertyInfo() = myDataList.GetType.GetProperties()

Dim colname as string()
Redim colname(ubound(props)
For i = 0 to ubound(props)
colname(i) = props(i).Name
Next i

I get colname array as follows:
colname(0) = "DataView"
colname(1) = "Item"
colname(2) = "Item"
colname(3) = "Row"
colname(4) = "RowVersion"
colname(5) = "IsNew"
colname(6) = "IsEdit"
Where should be getting:
colname(0) = "Column1"
colname(1) = "Column2"
........

It seemed to work once then I must have changed something because it won't
work anymore. Any suggestions?
 
C

Cor Ligthert

Dennis,

I was looking at your problem and had some questions however it is for you
nigth when it is for me day. Than I looked what you was really doing and
thought that it could be done more simple.

This was what I made
\\\
Dim myarraylist As New ArrayList
if TypeOf obj Is DataView Then
For Each col As DataColumn In _
DirectCast(obj, DataView).Table.Columns
myarraylist.Add(col.ColumnName)
Next
Else
For Each col As DataColumn In _
DirectCast(obj, DataSet).Tables(v_DataMember).Columns
myarraylist.Add(col.ColumnName)
Next
End If
///

I hope this helps

Cor
 
G

Guest

Thanks Cor. That's the way I'll do it but I still wonder what's wrong with
the other code. I had gotten that technique from an article by Rockford
Lhotka on creating a Data Bound LIstView control. I had it working and then
did something stupid and couldn't get it to work again. Darn if I know what
I accidentally changed though. The code is pretty simple and works for
ArrayLists and Array's of Classes and STructures as well.
 
K

kevin

You are receiving the names of the properties of the DataList objject
(IListSource). Instead of attempting to get the names in this manner,
why not just use the DataView or DataTable as your source object.
Meaning - DataTable has a DefaultView Property which will allow you to
simply use only DataViews, or you can get the Source DataTable from the
DataView and use the DataTable only. Are you using any additional
properties of the IListSource Interface, or do you only need the Column
Names?

Kevin M. Schreiner
VP Software Architecture
Business Intelligence Force (bi4ce)
 
G

Guest

Thanks for reply. I need property/field names as well as Canread, canwrite.
My routine also accepts Arraylists and Array's of Structures and Classes. I
had the damn code working at one time but must have messed it up and for the
life of me I can't figure it out. I know how to do it using If statements
for TypeOf for DataView's, Datatables, DatsSets, etc. but the beauty of this
code was that it should work for any Ilist which all of these implement.
 
C

Cor Ligthert

Dennis,

Maybe now you tell this I will try it in the weekend, not today anymore.
However my question was what type is "myDataList".

I have seen this one as well in C# code. And once did it in VBNet however I
did not save it.

No promises of course.

Cor
 
G

Guest

myDataList is dimensioned as

Dim myDataList as IList

Thanks for help. Hope you can get it working.
 

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