简体   繁体   中英

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 mousedoubleclick event.

Problem1 is that every time I double click on a row, it's always the first record that is beeing copied. In the database there are 451 records.

My plan is that if I doubleclick on ex record 201 that recorde 201 is copied, but no thats not the case.

Problem 2 and 3 will I ask later, I think that problem 10 and 2 will be solved automaticly when problem 1 is solved.

Can anyone help me pls, i'm desperade

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.
}

a red line under these?

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

what about this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM