Click here to Skip to main content
12,355,591 members (69,750 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
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..




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 2-Jan-13 23:53pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

try this..
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;
        }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

hey try below code..
DataTable yourDataTable = yourGridView.DataSource as DataTable
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100