Want to put DataGridView DataSource into a DataTable from Base Form

superservo15

Member
Joined
Apr 25, 2006
Messages
11
Location
Canada
Programming Experience
10+
Hi,
I am having problems figuring this one out and am hoping someone here might know ;)

I have a base form and I am trying to get the DataSource of the ActiveControl if the control is a datagridview.

What I have so far is:

VB.NET:
Dim curControl As Control = Me.ActiveControl
Dim ControlType As String = curControl.GetType.ToString()

If ControlType = "System.Windows.Forms.DataGridView" Then
                'get the data out from datagridview to a datatable
                Dim dtGridData As DataTable 
                dtGridData = ???
End If

I can't seem to get the dataSource from the control object, but am unsure how to access the datagridview from the base form. Any ideas or suggestions would be great!

Thanks in advance.
 
VB.NET:
Dim activeGrid = TryCast(Me.ActiveControl, DataGridView)

If activeGrid IsNot Nothing Then
   Dim table = DirectCast(activeGrid.DataSource, DataTable)

   'Use table here.
End If
This assumes that you did actually bind a DataTable in the first place, and not a DataSet or a DataView.
 
Thanks this was exactly what I wanted and worked like a charm!

Sorry about my profile, clearly I don't post questions enough to notice that stuff. I will make sure it is up to dare NEXT time I post.. although what if I use multiple frameworks?!? J/k :)
 
although what if I use multiple frameworks?!? J/k :)
The forum profile setting is for current 'primary development framework'. Just write a line in your thread to explain what platform is relevant when the primary one isn't. Simple? :)
 
Back
Top