Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to get the Gridview data into DataTable [modified]

How to get the Gridview data into DataTable [modified]

Scheduled Pinned Locked Moved ASP.NET
wpfwcfdesigntutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    K V Sekhar
    wrote on last edited by
    #1

    Hi all, How to get the Gridview data (after binding the gridview) into DataTable ? I tried in the following way. But its giving Exception Unable Convert 'System.Web.UI.WebControls.GridViewRowCollection' to 'System.Data.DataRow' DataTable dt= new DataTable(); foreach(DataRow drow in Gridview1.Rows) { dt.Rows.Add(drow); } Please suggest me how to do that. Thanks in advance.

    modified on Wednesday, October 29, 2008 2:32 AM

    M S 2 Replies Last reply
    0
    • K K V Sekhar

      Hi all, How to get the Gridview data (after binding the gridview) into DataTable ? I tried in the following way. But its giving Exception Unable Convert 'System.Web.UI.WebControls.GridViewRowCollection' to 'System.Data.DataRow' DataTable dt= new DataTable(); foreach(DataRow drow in Gridview1.Rows) { dt.Rows.Add(drow); } Please suggest me how to do that. Thanks in advance.

      modified on Wednesday, October 29, 2008 2:32 AM

      M Offline
      M Offline
      meeram395
      wrote on last edited by
      #2

      Try this: foreach(GridViewRow drow in GridView1.Rows) { dt.Rows.Add(drow); }

      Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

      K 3 Replies Last reply
      0
      • M meeram395

        Try this: foreach(GridViewRow drow in GridView1.Rows) { dt.Rows.Add(drow); }

        Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

        K Offline
        K Offline
        K V Sekhar
        wrote on last edited by
        #3

        Thanks for your suggestion. I will try that.

        1 Reply Last reply
        0
        • K K V Sekhar

          Hi all, How to get the Gridview data (after binding the gridview) into DataTable ? I tried in the following way. But its giving Exception Unable Convert 'System.Web.UI.WebControls.GridViewRowCollection' to 'System.Data.DataRow' DataTable dt= new DataTable(); foreach(DataRow drow in Gridview1.Rows) { dt.Rows.Add(drow); } Please suggest me how to do that. Thanks in advance.

          modified on Wednesday, October 29, 2008 2:32 AM

          S Offline
          S Offline
          S Dhanasekaran
          wrote on last edited by
          #4

          Try this DataTable workTable= new DataTable(); DataRow workRow = workTable.NewRow(); foreach(DataRow workRow in Gridview1.Rows) { workRow["Fields"] = //your gridview rows dt.Rows.Add(workRow); }

          1 Reply Last reply
          0
          • M meeram395

            Try this: foreach(GridViewRow drow in GridView1.Rows) { dt.Rows.Add(drow); }

            Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

            K Offline
            K Offline
            K V Sekhar
            wrote on last edited by
            #5

            Hello Meeram395, I tried. But i got exception System.ArgumentException: Input array is longer than the number of columns in this table. foreach(GridViewRow drow in GridView1.Rows) { dt.Rows.Add(drow); //Here i got the above exception }

            1 Reply Last reply
            0
            • M meeram395

              Try this: foreach(GridViewRow drow in GridView1.Rows) { dt.Rows.Add(drow); }

              Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

              K Offline
              K Offline
              K V Sekhar
              wrote on last edited by
              #6

              Hello meeram395, I solved the that exception. But I am getting empty datarows. DataItem : null My logic: BindGridview(); DataTable dt = new DataTable(); dt.Columns.Add(); foreach (GridViewRow dr in GvReport.Rows) { dt.Rows.Add(dr); // I am getting the row count as 10. But DataItem is null }

              M 1 Reply Last reply
              0
              • K K V Sekhar

                Hello meeram395, I solved the that exception. But I am getting empty datarows. DataItem : null My logic: BindGridview(); DataTable dt = new DataTable(); dt.Columns.Add(); foreach (GridViewRow dr in GvReport.Rows) { dt.Rows.Add(dr); // I am getting the row count as 10. But DataItem is null }

                M Offline
                M Offline
                meeram395
                wrote on last edited by
                #7

                I have tried as below and getting the results successfully:

                DataTable dt = new DataTable();

                    dt.Columns.Add();
                    dt.Columns\[0\].ColumnName = "User Name";        
                    dt.Columns.Add();
                    dt.Columns\[1\].ColumnName = "User Email";
                
                    foreach (GridViewRow row in SearchGrid.Rows) // SearchGrid is the name of the Grid.
                    {
                        
                        DataRow dr = dt.NewRow();
                        dr\["User Name"\] = row.Cells\[1\].Text;
                        dr\["User Email"\] = row.Cells\[2\].Text;
                        dt.Rows.Add(dr);
                    }
                

                Hope this helps.

                Success is the good fortune that comes from aspiration, desperation, perspiration and inspiration.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups