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. General Programming
  3. C#
  4. copy Datagridview value to another datagridview

copy Datagridview value to another datagridview

Scheduled Pinned Locked Moved C#
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.
  • A Offline
    A Offline
    annie_bel
    wrote on last edited by
    #1

    hi!! I have a datagridview in a form. i want to send all the data from the first datagridview to another datagridview, which is present on another form. Best Regards,

    M M 2 Replies Last reply
    0
    • A annie_bel

      hi!! I have a datagridview in a form. i want to send all the data from the first datagridview to another datagridview, which is present on another form. Best Regards,

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

      Which part are you have trouble with? Have you worked out how to communicate with another form yet? (e.g. call a function in another form?)

      Life goes very fast. Tomorrow, today is already yesterday.

      A 1 Reply Last reply
      0
      • M musefan

        Which part are you have trouble with? Have you worked out how to communicate with another form yet? (e.g. call a function in another form?)

        Life goes very fast. Tomorrow, today is already yesterday.

        A Offline
        A Offline
        annie_bel
        wrote on last edited by
        #3

        no, actually i couldn't understand how to get data one by one and copy to second DGV.

        M 1 Reply Last reply
        0
        • A annie_bel

          no, actually i couldn't understand how to get data one by one and copy to second DGV.

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          Well assuming you have the same columns set up in both tables then you can iterate through the Rows property and add each one. Something like...

          foreach(DataGridViewRow row in dgv1.Rows)
          {
          dgv2.Rows.Add(row);
          }

          You would obviously need to pass the first DGV's Rows collection to the second form to be processed

          Life goes very fast. Tomorrow, today is already yesterday.

          modified on Thursday, November 11, 2010 8:58 AM

          A 1 Reply Last reply
          0
          • M musefan

            Well assuming you have the same columns set up in both tables then you can iterate through the Rows property and add each one. Something like...

            foreach(DataGridViewRow row in dgv1.Rows)
            {
            dgv2.Rows.Add(row);
            }

            You would obviously need to pass the first DGV's Rows collection to the second form to be processed

            Life goes very fast. Tomorrow, today is already yesterday.

            modified on Thursday, November 11, 2010 8:58 AM

            A Offline
            A Offline
            annie_bel
            wrote on last edited by
            #5

            when i insert your code the following errors occured.

            No row can be added to a DataGridView control that does not have columns. Columns must be added first

            . and when i insert column by following code:

            foreach (DataGridViewColumn c1 in dataGridView1.Columns)
            {
            DataGridViewColumn c2 = (DataGridViewColumn)c1.Clone();

                        dataGridView2.Columns.Add(c2);
                    }
            

            this error occured:

            Row provided already belongs to a DataGridView control.

            M 1 Reply Last reply
            0
            • A annie_bel

              when i insert your code the following errors occured.

              No row can be added to a DataGridView control that does not have columns. Columns must be added first

              . and when i insert column by following code:

              foreach (DataGridViewColumn c1 in dataGridView1.Columns)
              {
              DataGridViewColumn c2 = (DataGridViewColumn)c1.Clone();

                          dataGridView2.Columns.Add(c2);
                      }
              

              this error occured:

              Row provided already belongs to a DataGridView control.

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #6

              OK my mistake, something like this should work...

              foreach (DataGridViewRow row in dgv1.Rows)
              {
              object[] cells = new object[row.Cells.Count];
              row.Cells.CopyTo(cells, 0);
              dgv2.Rows.Add(cells);
              }

              Life goes very fast. Tomorrow, today is already yesterday.

              1 Reply Last reply
              0
              • A annie_bel

                hi!! I have a datagridview in a form. i want to send all the data from the first datagridview to another datagridview, which is present on another form. Best Regards,

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                What is the collection that is used to populate the DGV, datatable, List<>, array[] or is the DGV manually created with no underlying collection.

                Never underestimate the power of human stupidity RAH

                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