|
-
Mar 20th, 2006, 09:56 PM
#1
Thread Starter
Hyperactive Member
[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
Last edited by hyper88; Mar 21st, 2006 at 02:47 AM.
-
Mar 20th, 2006, 10:02 PM
#2
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
Last edited by jmcilhinney; Mar 21st, 2006 at 02:33 AM.
Reason: Fixed first code snippet
-
Mar 21st, 2006, 02:23 AM
#3
Thread Starter
Hyperactive Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|