DataGridView / get bound table name?

Using vb.net with DataGridView.
The DataGridView is bound to a table. How can I get the name of the bound data table from the DataGridView?
LVL 1
Jess31Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
you may try this;

YourDataGridView.DataMember

Open in new window


OR

Dim src As BindingSource = YourDataGridView.DataSource
        MessageBox.Show(src.DataMember)

Open in new window

0
Jess31Author Commented:
Ryan,
first method returns ""

second one gives this error:
Unable to cast object of type 'System.Data.DataTable' to type 'System.Windows.Forms.BindingSource'.
0
ArkCommented:
MsgBox(CType(DataGridView1.DataSource, DataTable).TableName)

Open in new window

0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Jess31Author Commented:
returns ""
0
ArkCommented:
Complete solution for datagrid binded to bindingsource/dataset/datatable (not supporting LINQ classes)
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

Open in new window

0
Jess31Author Commented:
still getting ""
And I am binding a DataTable. Just like this DataGridView1.DataSource = dt
0
ArkCommented:
How do you create dt?
PS. You must set datatable name:
Dim dt As New DataTable("Test")
or
Dim dt As New DataTable()
dt.TableName = "Test"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
MsgBox(CType(DataGridView1.DataSource, DataTable).TableName)

Open in new window

that posted by Ark should get the name you wanted.

make sure before you bind your DataTable to your DataGridView, you got set its TableName accordingly?

like:

Dim dt As New DataTable
        dt.TableName = "test 123"
        dt.Columns.Add("ID", GetType(Integer))
        dt.Columns.Add("Name", GetType(String))
        dt.Columns.Add("Date", GetType(DateTime))

        dt.Rows.Add(1, "John", DateTime.Now)
        dt.Rows.Add(2, "Peter", DateTime.Now)

        DataGridView1.DataSource = dt
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
DataGridView

From novice to tech pro — start learning today.