Click here to Skip to main content
15,917,455 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am working in VB.NET 2010.
I want to shift all the data in a datatable.
In short you can say that i want to convert my datagridview into a datatable along with the data !

How can i do this ?

Can anyone help me ?

What I have tried:

I tried ....

VB
Public Sub Grid_to_DataTable(myGrid As DataGridView, myDataTable As DataTable)
        Try
            Dim Cols As Integer = 0
            While Cols <= myGrid.ColumnCount - 1
                myDataTable.Columns.Add(myGrid.Columns(Cols).Name, myGrid.Columns(Cols).CellType)
                Cols = Cols + 1
            End While
            myDataTable = myGrid.DataSource

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Grid_to_DataTable", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
Posted
Updated 9-Apr-16 20:14pm
v2
Comments
Patrice T 9-Apr-16 18:41pm    
And you have a problem with this code ?

1 solution

Try This

VB.NET
Dim NewTable As DataTable = DirectCast(DataGridView1.DataSource, DataTable)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900