Copy DataGridView to DataTable (Disconnected)

  • Thread starter Thread starter DIOS
  • Start date Start date
D

DIOS

Is there a way to copy the contents of a DataGridView to a DataTable?
I only want the headers and contents copied and not the connection
that the DGV has. In other words something like copying the contents
but not the database connection. The datatable will stand on its own
as if it were manually filled.

p.s. This is not binding a datatable to a DGV. But instead the other
way, copying contents from a DGV to a datatable.

tia
AGP
 
The DGV really does not have a connection to any data source. It just
displays any data that you put into it. DataTables, lists, arrays etc. How
are you getting the data into the grid?

David


======================================
David McCarter [Microsoft MVP]
www.dotNetTips.com
David McCarter''''s .NET Coding Standards available at:
http://codingstandards.notlong.com
 
Is there a way to copy the contents of a DataGridView to a DataTable?
I only want the headers and contents copied and not the connection
that the DGV has. In other words something like copying the contents
but not the database connection. The datatable will stand on its own
as if it were manually filled.

p.s. This is not binding a datatable to a DGV. But instead the other
way, copying contents from a DGV to a datatable.

tia
AGP

The data gets into the DGV by first an adapter to either MS-SQL or
Oracle database. A temp datatable is then filled with the returned
data. This temp table is returned as a function value and then I take
that table and bind it to the DGV. The DGV is filled in with various
sources but in the end I modify the headers so they are standard. Once
this is done then another functiion will take the data in the DGV and
process it further. The second routine is already coded to accept a
datatable so in this case I want to convert the data in the DGV to a
datatable and feed it to the second function. When I do this I notice
that the headers and field names are different so I cahnge them but it
seems to affect the data and thats why i think its still attached to
the db.

AGP
 
The data in the DGV is already a DataTable, you don't need to convert it. My
suggestion is to either keep a reference to the DT in your code or just get
it from the DataSource of the DGV.

David


======================================
David McCarter [Microsoft MVP]
www.dotNetTips.com
David McCarter''''s .NET Coding Standards available at:
http://codingstandards.notlong.com
 
Is there a way to copy the contents of a DataGridView to a DataTable?
I only want the headers and contents copied and not the connection
that the DGV has. In other words something like copying the contents
but not the database connection. The datatable will stand on its own
as if it were manually filled.

p.s. This is not binding a datatable to a DGV. But instead the other
way, copying contents from a DGV to a datatable.

tia
AGP

not that easy. the two routines are in different places and I am not
touching the second routine which takes its argument as a datatable,
that has to stay that way. but i found a way around my problem which
was changing the column.name to my required name. i was changing the
SourceField and that was changing the column data.

AGP
 
Back
Top