MsgBox(CType(DataGridView1.DataSource, DataTable).TableName)
Private Function GetBoundTableName(dgv As DataGridView) As String
If dgv.DataSource Is Nothing Then Return ""
Dim dt As DataTable = Nothing
If TypeOf dgv.DataSource Is DataTable Then
dt = CType(dgv.DataSource, DataTable)
ElseIf TypeOf dgv.DataSource Is DataSet Then
dt = GetDsDataTable(CType(dgv.DataSource, DataSet), dgv.DataMember)
ElseIf TypeOf dgv.DataSource Is BindingSource Then
dt = GetBsDataTable(CType(dgv.DataSource, BindingSource))
End If
If dt Is Nothing Then Return ""
Return dt.TableName
End Function
'Get datatable binded to bindingsource
Private Function GetBsDataTable(ByVal bs As BindingSource) As DataTable
If TypeOf bs.DataSource Is DataTable Then Return CType(bs.DataSource, DataTable)
Return GetDsDataTable(GetBsDataSet(bs), bs.DataMember)
End Function
'Get dataset binded to bindingsource
Private Function GetBsDataSet(ByVal bs As BindingSource) As DataSet
If TypeOf bs.DataSource Is DataSet Then Return CType(bs.DataSource, DataSet)
If TypeOf bs.DataSource Is BindingSource Then Return GetBsDataSet(CType(bs.DataSource, BindingSource))
Return Nothing
End Function
'Get datatable in dataset (either as table or relation)
Private Function GetDsDataTable(ds As DataSet, tableName As String) As DataTable
If ds Is Nothing OrElse tableName = "" Then Return Nothing
If ds.Tables.Contains(tableName) Then Return ds.Tables(tableName)
If ds.Relations.Contains(tableName) Then Return ds.Relations(tableName).ChildTable
Return Nothing
End Function
MsgBox(CType(DataGridView1.DataSource, DataTable).TableName)
that posted by Ark should get the name you wanted.
Open in new window
OR
Open in new window