Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can anyone give me an idea of how to copy selected rows from one datagridview to another datagridview provided by the order rows should be displayed in the same order. Like the selected rows in from DGV1 will be

row 1
row 2
row 3


the DGV2 also display the copied rows in same order as above not like

row 3
row 2
row 1


Thanks in advance
Posted

 
Share this answer
 
I assume this is happening because you are looping through the source DGV and using the Add method to append them to the destination?
The easiest way is not to use Add, but Insert instead - that way you can specify exactly where in the Rows list you want the items.
 
Share this answer
 
Comments
CyborgForever 8-Nov-11 23:34pm    
Can u pls help me out in the syntax. Because what I've done is I added the selected rows to a Datatable and then bound the DT to DGV.
OriginalGriff 9-Nov-11 4:02am    
The DataTable.Rows Collection also has an Insert method...
CyborgForever 9-Nov-11 6:07am    
I tried the insertion(ref to my above solution) but this does'nt work when the datagridview from which I'm getting the selected rows is not bound.
C#
selectedRowCount = dgv.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                {
                    DataRow[] dRows = new DataRow[dgv.SelectedRows.Count];
                    for (int i = 0; i < dgv.SelectedRows.Count; i++)
                        dRows[i] = ((DataRowView)dgv.SelectedRows[i].DataBoundItem).Row;
                    foreach (DataRow dgvRow in dRows)
                    {
                        dgvNew.Rows.Insert(0, dgvRow.ItemArray);
                    }
                    dgvNew.Refresh();

                }
 
Share this answer
 
v2

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