0
votes

I have two individual datagidview with data, DGV1 and DGV2. Now I want to make a 3rd datagrid DGV3, copy over the columns "LIST", "Theta" and "Phi", but for my last column in DGV3 ("Ampl (dB)"), I have to add the values in DGV1 and DGV2 and make it as a input to DGV Total. Assuming i there is two full datagridview, how can I handle my problem? Any help is strongly apricheded. the data comes from two individual Arduinos. The population of DGV1 and DGV2 happens in two EventHanldlers.

delegate void dataToGrid3(int deg, int de2, double ampl_Total, int seq);
private void adddataToGrid3(int deg, int deg2, double ampl_Total, int seq)
    {
        int row = 0;
        dataGridView3.Rows.Add();
        row = dataGridView3.Rows.Count - 2;
        dataGridView3["LIST3", row].Value = seq;
        dataGridView3["Theta3", row].Value = deg;
        dataGridView3["Phi3", row].Value = deg2;
        dataGridView3["Ampl_dB3", row].Value = ampl_Total;
    }

Trying to do use button to populate DGV Total:

private void DGV_Total_Btn_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < runCounter; i++)
        {

        }
    }

How in looks

1

1 Answers

0
votes

You can iterate over all DataGridViewRows from the first DataGridView DGV1. There you copy 'List' 'Theta' and 'Phi' from DGV1 directly in the third DataGridView DGV3. With using your row index you can get the value of each "Ampl (dB)" from both DataGridViews and sum them.

private void DGV_Total_Btn_Click(object sender, EventArgs e)
{
    DGV3.Rows.Clear();
    int yourColumnIndex = 3;    // column index from "Ampl (dB)"
    double total = 0;
    foreach (DataGridViewRow row in DGV1.Rows)
    {
        total = Convert.ToDouble(row.Cells[yourColumnIndex].Value) + Convert.ToDouble(DGV2.Rows[row.Index].Cells[yourColumnIndex].Value);
        adddataToGrid3(row.Cells[1].Value, row.Cells[2].Value, total, row.Cells[0].Value);
    }
}

You can also add the data directly in the foreach loop, which would look like this:

private void DGV_Total_Btn_Click(object sender, EventArgs e)
{
    DGV3.Rows.Clear();
    int yourColumnIndex = 3;    // column index from "Ampl (dB)"
    double total = 0;           
    foreach(DataGridViewRow row in DGV1.Rows)
    {
        total = Convert.ToDouble(row.Cells[yourColumnIndex].Value) + Convert.ToDouble(DGV2.Rows[row.Index].Cells[yourColumnIndex].Value);
        DGV3.Rows.Add(new object[] { row.Cells[0].Value,    //copy List
                                     row.Cells[1].Value,    //copy Theta
                                     row.Cells[2].Value,    //copy Phi
                                     total });              //add total "Ampl (dB)"
    }
}