I'm getting this error: Argument 'Expression' cannot be converted to type 'DataGridViewRow'.
I have no clue what it means or how to fix it, it happens at this line:
dt2.Rows(Val(selectedItem))("Position") = dt.Rows(selectedItem.Cells(1).Value)("Mouse Position")
Can someone please explain what the error is and how to fix it?
Try
If selectedItems Is Nothing Then
For n = 0 To dt.Rows.Count - 1
dt2.Rows.Add(n)
dt2.Rows(n)("Position") = dt.Rows.Item(n)("Mouse Position")
Next
Else
For Each selectedItem As DataGridViewRow In selectedItems
dt2.Rows.Add(selectedItem)
dt2.Rows(Val(selectedItem))("Position") = dt.Rows(selectedItem.Cells(1).Value)("Mouse Position")
Next
End If
Catch ex As Exception
MsgBox("Error", MsgBoxStyle.Exclamation, "Error!")
End Try
I have had to look at your previous question to understand your question.
The variable dt2 is a DataTable with only one column called "Position", so adding a DataGridViewRow to the DataRow collection of this DataTable makes no sense.
Your first loop should be
For n = 0 To dt.Rows.Count - 1
Dim r = dt2.NewRow();
r("Position") = dt.Rows.Item(n)("Mouse Position")
dt2.Rows.Add(r)
Next
while the second loop
For Each selectedItem As DataGridViewRow In selectedItems
Dim r = dt2.NewRow()
r("Position") = dt.Rows(selectedItem.Cells(1).Value.ToString)("Mouse Position")
dt2.Rows.Add(r)
Next