Dim cells = From Qmaxminslist As DataGridViewRow In DataGridView2.Rows.Cast(Of DataGridViewRow)() Take Integer.Parse(daysamaxminsmv.Text)
For Each row As DataGridViewRow In cells
// piece of code...
Next
I have converted this code in C# as:
dynamic cells = from Qmaxminslist in DataGridView2.Rows.Cast<DataGridViewRow>() select (int.Parse(daysamaxminsmv.Text));
foreach (DataGridViewRow row in cells)
{
// piece of code...
}
but here comes an exception in the foreach statement saying:
"Cannot convert type 'int' to 'System.Windows.Forms.DataGridViewRow'"
how to solve it? any help will be appreciated. Thanks
You haven't converted it correctly. The VB code selects DataGridViewRows
, the C# code selects ints
. You want to take N rows. VB.NET supports Enumerable.Take
directly in the LINQ query, C# does not support it. So i'd use method syntax in C#.
So this works as desired (don't translate Dim
with dynamic
but var
):
int takeRows = int.Parse(daysamaxminsmv.Text);
var rows = DataGridView2.Rows.Cast<DataGridViewRow>().Take(takeRows);
foreach (DataGridViewRow row in rows)
{
// piece of code...
}