Link to home
Start Free TrialLog in
Avatar of jdcoburn
jdcoburn

asked on

DataGridViewRowCollection

hi - I'm using C# and ,net 4.
I'm trying to implement a copy and paste from a datagridview to a datagridview by row. it would seem that i can use the DataGridViewRowCollection that is returned from property "SelectedRows". after acquiring the collection, i use the method "CopyTo" to copy to an array of DGV rows. the count is correct. the documentation says "Copies the elements of the collection to the specified array, starting at the specified index."
but i then try to extract the cell value from each item in the collection and its null.
note that i'm using the cell index since it doesn't find the column name and throws an error. there are 8 items in teh row.

the code is below:
foreach (DataGridViewRow dgvRow in copyRows.AsEnumerable())
                    {

                        DataRow drg = CurrentScriptTable.NewRow();

                        drg["script_Id"] = currentScript;
                        drg["lineNumber"] = scriptDisplay_DGV.CurrentRow.Index + k;
                        drg["label"] = dgvRow.Cells[2].Value.ToString();

                        CurrentScriptTable.Rows.InsertAt(drg, scriptDisplay_DGV.CurrentRow.Index + k++);
                    }
any suggestions?
Avatar of jdcoburn
jdcoburn

ASKER

hi -- an additional piece of information that occurs to me and may be the cause of my problem - the datagridview is data bound. I wonder if copying the selection outside of the original de-references the collection from the datatable it is bound to? therefore the values are not available when i go to paste them.
Jim
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial