Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

im trying to pass data from one datagridview to another on a button click.In order for the data to be passed a checkbox has to be checked.I am able to do this using datagridview.SelectedRow(see below 1). im trying to do it using datagridview.Rows(see below 2) .

My question is why does #2 only create one row in the second datagridview?

What I have tried:

#1.
C#
List<object> destList = new List<object>();
            foreach (DataGridViewRow row in dataGridView5.SelectedRows)
                destList.Add(row.DataBoundItem);
            dataGridView2.DataSource = destList;


//Data gets passed to other datagridview

#2.
C#
foreach (DataGridViewRow row in dataGridView5.Rows)
{

    DataGridViewCheckBoxCell cell = row.Cells[0] as DataGridViewCheckBoxCell;

    //We don't want a null exception!
    if (cell.Value != null)
    {
        if (Convert.ToBoolean(row.Cells[0].Value))
        {
            destList.Add(row.DataBoundItem);
            dataGridView2.DataSource = destList;
        }
    }

//Data gets passed to other datagridview, but only one row is created??
Posted
Comments
Richard Deeming 29-Jun-16 12:47pm    
Try moving the line that sets the DataSource outside of the foreach loop.
BEBE2011 29-Jun-16 12:51pm    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900