Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m having a gridview and i want to get all the values of the gridview to the datatable
pls help me for this above question

Thanks
Posted

It may vary depending upon the DataSource of your GridView.
Try:
C#
//If DataSource of GridView1 is a DataTable
DataTable dt = (DataTable)GridView1.DataSource;

//If DataSource of GridView1 is a DataView
DataView dv = (DataView)GridView1.DataSource; 


If you want to make some changes while porting the data, you have to loop through. But, you cannot convert GridViewRow to DataRow. You have to manually create columns first, then loop through all the rows. Within this loop, you would need to loop through all the columns of the row and copy it to the DataRow that you are creating.
C#
//This is just a Psuedocode
DataTable dt = new DataTable();
//Add columns here
dt.Columns.Add(); //or something similar
//go on till all the columns are added
//loop and second loop
foreach(GridViewRow gvr in GridView1.Rows)
{
   DataRow dr = dt.NewRow();
   
   //second loop - looping all columns
   for(int i = 0; i < gvr.Rows.Count; ++i)
   {
      dr[i] = gvr.Cells[i].Text;
   }
} 
 
Share this answer
 
Try this code:I am sure it will help you

Sqlconnection con=new Sqlconnection("con string");
String Query="Select * from <Table name> where <Condition>";

SqlDataAdapter da=new SqlDataAdater(Query,Con);
DataSet ds=new DataSet();
da.fill(ds,"dx-army");

GridView1.dataSource=ds.Tables["dx-army"];
GridView1.dataBind();


If you got answer then please do not forget to vote me....


thanks

DX-ARMY
 
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