Click here to Skip to main content
12,395,234 members (65,847 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
Hi coders,
My question is that am trying to copy the values of a datagridview on one form to another datagridview on the second form. I have tried the code below but it is only copying the first row. Please guide me on realizing where i wrote the code wrongly.
Here is the code;
Code on the first form
public Sales()
        {
            InitializeComponent();
            initializeDgvFields();
            loadSampleValueAtDgv1();
        }
        DataTable dt;
        private void initializeDgvFields()
        {
            dt = new DataTable();
            dt.Columns.Add("Item Name");
            dt.Columns.Add("Shop Qty");
            dt.Columns.Add("Price");
            dt.Columns.Add("Quantity");
            dt.Columns.Add("Total");
      
 
        }
        private void loadSampleValueAtDgv1()
        {
            for (int i = 0; i < dataGridView2.Rows.Count; ++i)
            {
               dt.Rows.Add();
            }
        }
 
        private void copyDgv1ValueToDgv2()
        {
for (int i = 0; i < dt.Rows.Count; i++)
            {
                f2.dt2.Rows.Add(dataGridView2.Rows[i].Cells[0].Value.ToString(), dataGridView2.Rows[i].Cells[1].Value.ToString(),
                    dataGridView2.Rows[i].Cells[2].Value.ToString(), dataGridView2.Rows[i].Cells[3].Value.ToString(), dataGridView2.Rows[i].Cells[4].Value.ToString());
            }
 
        }
 
        Close_Sale f2;
        private void button1_Click(object sender, EventArgs e)
        {
            f2 = new Close_Sale(this);
            copyDgv1ValueToDgv2();         
            f2.Show();
        }
code on the second form
Sales f1;
 
      public Close_Sale(Sales f2)
      {
          InitializeComponent();
          f1 = f2;
          initializeDgvFields();
 
      }
      public DataTable dt2;
      private void initializeDgvFields()
      {
          dt2 = new DataTable();
          dt2.Columns.Add("Item Name");
          dt2.Columns.Add("Shop Qty");
          dt2.Columns.Add("Price");
          dt2.Columns.Add("Quantity");
          dt2.Columns.Add("Total");
          this.dataGridView1.DataSource = dt2;
 
      }
Thanks for any help offered
Posted 27-Dec-12 20:43pm
Xonel516

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

Use the DataTable.Copy[^] function:
private void copyDgv1ValueToDgv2()
{
     ft.dt2 = dt.Copy();
}
And the initializeDgvFields function:
private void initializeDgvFields()
       {
           this.dataGridView1.DataSource = dt2;
       }
Hope this helps.
  Permalink  
v2
Comments
Xonel 28-Dec-12 3:14am
   
Thank you for responding
But when i add
f2.dt2 = dt.copy();
It actually transfers zero data unlike before when i was viewing the first row only
Please guide me from there...Thank you
ProgramFOX 28-Dec-12 3:16am
   
Hi, try
ft2.dt2 = dt.Clone();

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
Web01 | 2.8.160721.1 | Last Updated 28 Dec 2012
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