Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to pass datagridview multiple rows from one form to another datagridview. Its working fine for current row but generates an error for multiple rows. I.e not all paths return a value.

What I have tried:

get
{
foreach (DatagridviewRow r in dgv.Rows)
{return row.cells[2].value. ToString();}
}
set
{
.............
}
Posted
Updated 27-Jan-19 18:51pm
Comments
Bryian Tan 27-Jan-19 16:51pm    
Have you try modify it to return List / array of string instead of a string?
Robert S4r 27-Jan-19 17:41pm    
Not yet. No idea on how to go about it.

You can't return multiple values from a property unless you return a collection - adding return in a loop will terminate the loop a return a single value*. So the compiler throws an error because if there are no items in the DGV then there is no way to reach a return statement and the property can't compile.

* Well, probably there is - using yield return but I've never tried that in a property, and it's going to be complicated to arrange.
 
Share this answer
 
If datagridview datasource is a datatable...


C#
//get a datasource
DataTable dt = (DataTable)DataGridView1.DataSource;
//filter data
DataGridView2.DataSource = dt.Filter("Field=whatever");
//or 
DataGridView2.DataSource = dt.AsEnumerable().Select(x=>x.Field<DataTypeOfField>("FieldName")).CopyToDataTable();
 
Share this answer
 

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