How to join values from 2 datagrid’s to only 1, even though these columns have different sizes?

Asked

Viewed 20 times

0

        string[] datasald = new string[dataGridView1.Rows.Count + dataGridView2.Rows.Count - 2];
        string[] debitosald = new string[dataGridView1.Rows.Count + dataGridView2.Rows.Count - 2];
        string[] creditosald = new string[dataGridView1.Rows.Count + dataGridView2.Rows.Count - 2];

       

       

        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            datasald[i] = dataGridView1.Rows[i].Cells[0].Value.ToString();
            debitosald[i] = dataGridView1.Rows[i].Cells[1].Value.ToString();
            creditosald[i] = dataGridView1.Rows[i].Cells[2].Value.ToString();                

        }

        int x = dataGridView1.Rows.Count-1;
        for (int i = 0; i < dataGridView2.Rows.Count-1 ; i++)
        {
            datasald[x] = dataGridView2.Rows[i].Cells[0].Value.ToString();
            debitosald[x] = dataGridView2.Rows[i].Cells[1].Value.ToString();
            creditosald[x] = dataGridView2.Rows[i].Cells[2].Value.ToString();
            x++;
        }


        for (int i = 0; i < datasald.Length-1; i++)
        {                                
        dataGridView4.Rows.Add(new object[] { datasald[i], debitosald[i], creditosald[i], datasald[x], debitosald[x], creditosald[x] });                
            x++;
        }

I have this code but I get this error: System.Indexoutofrangeexception: 'The index was outside the matrix boundaries.'

  • I came to the conclusion that I cannot join 2 datagrids with columns of different sizes, so I created a method to insert blank rows to the smaller datagrid until its size is the same as the larger one, after which I put the values of the two together and add to the third datagrid. If anyone has a better solution, I thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.