简体   繁体   中英

How to take a row from one DataGridView and copy it to another?

I am working on a project with winform where i did a user to double click on a row of a data grid view that contains BookId BookName and Genre and it should be added to another Dgv like a shopping cart.

private void DgvSearchResult_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
    DgvCart.Rows.Add(DgvSearchResult.SelectedRows.OfType<DataGridView>().ToArray());           
}

But the code above copies an empty row.

I used two datagridviews, origDgv that contained the data I was copying from and copyDgv, the datagridview I copied the data to.

This is the CellDoubleClick event from the DataGridView origDgv: -

    private void origDgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {

        string[] rowData = new string[origDgv.Columns.Count];
        int iOffset = 0;

        foreach (DataGridViewCell dgvCell in origDgv.Rows[e.RowIndex].Cells)
        {
            if(dgvCell.EditedFormattedValue != null)
            {
                rowData[iOffset] = dgvCell.EditedFormattedValue.ToString();

            }
            iOffset++;
        }


        copyDgv.Rows.Add(rowData);
    }

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