Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Evening....

How to copy the data from datagridview to datatable in c# desktop application?

Plz help me....
Posted
Updated 9-May-11 2:34am
v2

That really depends on how you populated your DGV in the first place. In all cases, you can get the data out of the DataSource property of the DGV. If you added the data as a DataTable in the first place, then it's simply a matter of casting it back to a DataTable. If you added it as a list, then you will need to manually parse the data into a DataTable of your own devising.
 
Share this answer
 
Comments
Ankur\m/ 9-May-11 8:44am    
Correct, the point is get the DataSource property and cast/assign it to the Datatable.
My 5!
ambarishtv 9-May-11 8:54am    
My 5 :)
Wonde Tadesse 9-May-11 20:21pm    
Good answer.My 5
Prasanta_Prince 12-May-11 11:21am    
Good one . 5 from me.
try this :)

C#
foreach (DataGridViewRow dataRow in dataGridView1.Rows)
{
    DataRow dataBoundItem = ((DataRowView)dataRow.DataBoundItem).Row;
    dt.ImportRow(dataBoundItem);
}


here dt is datatable

OR
DataTable tbl = dataGridView1.DataSource as DataTable;
 
Share this answer
 
v3

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