[RESOLVED]Getting Column Names, Col and row count from datagrid
Hi,
I am trying to get the column names and col/row count from a datagrid but it seems that i am stucked. I have tried the following which i got from searching the forum but i got an error : An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication1.exe
Additional information: Specified cast is not valid.
VB Code:
'Dg is a datagrid
Dim dt As DataTable
dt = Dg.DataSource
MsgBox(dt.Rows.Count.ToString)
MsgBox(dt.Columns.Count.ToString)
thanks for your time
Re: Getting Column Names, Col and row count from datagrid
Have you actually assigned a DataTable to the DataSource of the grid? If so you should use:
VB Code:
dt = DirectCast(dg.DataSource, DataTable)
If not then you'll need to get the object from the DataSource as it is and get the DataTable from that. Here are examples if you've used a DataSet or a DataView as the DataSource:
VB Code:
'For DataSet.
dt = DirectCast(dg.DataSource, DataSet).Tables(dg.DataMember)
'For DataView.
dt = DirectCast(dg.DataSource, DataView).Table
Re: Getting Column Names, Col and row count from datagrid
Ok..got it.. thanks jmcilhinney for the help.
You have been really helpful as i see that you try to help whenever you can.
thanks to all the guys who took their time to read my problems. Really appreciate it.