Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
heyyy....

I have a grid with data..I want transfer this gridrows data to a datatable on a button click..

I tried but I don't get the values in grid.. here my code..




C#
foreach (GridViewRow row in grvBaseFareGraph.Rows)
             {
                 DataRow dr;
                 dr = dtn.NewRow();

                 for (int i = 1; i < row.Cells.Count; i++)
                 {
                     dr[i] = row.Cells[3].Text;
                 }
                 dtn.Rows.Add(dr);
             }



plz help...
Posted

try this..
C#
DataTable GetDataTable(GridView dtg)
        {
            DataTable dt = new DataTable();
 
            // add the columns to the datatable            
            if (dtg.HeaderRow != null)
            {
 
                for (int i = 0; i < dtg.HeaderRow.Cells.Count; i++)
                {
                    dt.Columns.Add(dtg.HeaderRow.Cells[i].Text);
                }
            }
 
            //  add each of the data rows to the table
            foreach (GridViewRow row in dtg.Rows)
            {
                DataRow dr;
                dr = dt.NewRow();
 
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    dr[i] = row.Cells[i].Text.Replace(" ", "");
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
 
Share this answer
 
hey try below code..
C#
DataTable yourDataTable = yourGridView.DataSource as DataTable
 
Share this answer
 
Comments
aravindnass 3-Jan-13 6:15am    
we are not getting any values in datasource.. the gridview currently loaded with data..
prashant patil 4987 3-Jan-13 6:32am    
see solution 2..

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