MarioV February 2016

passing cell value from 1 datagrid to another

I have a window form that has a datagridview and when i doubleclick on a record the whole row is copied to another datagridview.

This is the code for that

        {
        string strCon = "Data Source=*********\\MSSQL2014;Initial Catalog=Artikelen;Persist Security Info=True;User ID=sa;Password=*******";
        string strSQL = "select Artikelcode, Omschrijving, Verkoop_BTW_in, Verkoop_BTW, Verkoop_BTW_in from Artikelen";
        SqlDataAdapter sda = new SqlDataAdapter(strSQL, strCon);
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(sda);

        // Populate a new data table and bind it to the BindingSource.
        DataTable dt = new DataTable();
        sda.Fill(dt);
        dbBindSource.DataSource = dt;
        // finally bind the data to the grid
        DGVParent.DataSource = dbBindSource;
        string sEmpDetailsToUpdate = DGVParent[1, DGVParent.CurrentRow.Index].Value.ToString();
        //label1.Text = sEmpDetailsToUpdate + " aangekocht";
        foreach (DataGridViewColumn DGV_Parents_Column in DGVParent.Columns)
        {
            DGVChild.Columns.Add((DataGridViewColumn)DGV_Parents_Column.Clone());
        }
        DataGridViewRow row = new DataGridViewRow();
        for (int iCnt = 0; iCnt <= DGVParent.Rows.Count - 1; iCnt++)
        {
            if (DGVParent.Rows[iCnt].Cells[1].Value.ToString() == (sEmpDetailsToUpdate))
            {
                row = (DataGridViewRow)DGVParent.Rows[iCnt].Clone();
                int iColIndex = 0;
                foreach (DataGridViewCell cell in DGVParent.Rows[iCnt].Cells)
                {
                    row.Cells[iColIndex].Value = cell.Value;
                    iColIndex += 1;
                }
                DGVChild.Rows.Add(row);
                break;      // NO MATCHES FOUND. BAIL OUT.
            }
        }
        DGVChild.Focus();  // SET FOCUS ON THE CHILD.
    }

This is declared in the mousedoub

Answers


Bioukh February 2016

There are many problems in your code. Try using the CellMouseDoubleClick event instead :

private void DGVParent_CellMouseDoubleClick(Object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.RowIndex < 0)
        return;

    if (DGVChild.Columns.Count != DGVParent.Columns.Count)
    {
        DGVChild.Columns.Clear();
        foreach (DataGridViewColumn parentColumn in DGVParent.Columns)
        {
            DGVChild.Columns.Add((DataGridViewColumn)parentColumn.Clone());
        }
    }

    var parentRow = DGVParent.Rows[e.RowIndex];
    var childRow = (DataGridViewRow)parentRow.Clone();
    int iColIndex = 0;
    foreach (DataGridViewCell cell in parentRow.Cells)
    {
        row.Cells[iColIndex++].Value = cell.Value;
    }
    DGVChild.Rows.Add(childRow);

    DGVChild.Focus();  // SET FOCUS ON THE CHILD.
}


Daniele HoŇ°ek February 2016

a red line under these?

DGVChild.Columns.Add((DataGridViewColumn)parentColumn.Clone());

what about this?

DGVChild.Columns.Add(((DataGridViewColumn)parentColumn).Clone());

Post Status

Asked in February 2016
Viewed 3,730 times
Voted 4
Answered 2 times

Search




Leave an answer