I am attempting to store an exact copy of a DataGridViewRow into the same row's tag, everything works fine and the results are as it should be... except that when I try to retrieve the value using the column Name(at the last line of the code), it tells me that the column name doesn't exist. But if I use the index, everything works fine.
I have tried using the method CreateCells() or Clone() the structure of the original table but still does not work.
I could stick with using the index. But I would prefer to obtain the values by Column Name. is that possible?
''Create and populate a datagridview with one row
Dim DGV As New DataGridView
DGV.Columns.Add("No1", "No1")
DGV.Columns.Add("No2", "No2")
DGV.Columns.Add("No3", "No3")
Dim Objects As New List(Of Object)
Objects.Add("1")
Objects.Add("2")
Objects.Add("3")
DGV.Rows.Add(Objects.ToArray)
'' Loop to store a copy of itself into the Row's Tag
For Each selectedrows As DataGridViewRow In DGV.Rows
Dim DataGridViewRow As New DataGridViewRow
Dim CellArray As New List(Of Object)
DataGridViewRow.CreateCells(DGV) ''I have tried DataGridViewRow = selectedrow.clone and it still doesnt work
For Each Cell As DataGridViewCell In selectedrows.Cells
CellArray.Add(Cell.Value)
Next
DataGridViewRow.SetValues(CellArray.ToArray)
selectedrows.Tag = DataGridViewRow
Next
DGV.Rows(0).Cells(0).Value = "Change"
MessageBox.Show(DGV.Rows(0).Cells(0).Value) ''Works (Output: Change)
MessageBox.Show(DGV.Rows(0).Tag.Cells(0).Value) ''Works (Output: 1)
MessageBox.Show(DGV.Rows(0).Cells("No1").Value) ''Works (Output: Change)
MessageBox.Show(DGV.Rows(0).Tag.Cells("No1").Value) ''Doesnt Work (Output: System.ArgumentException: 'Column named No1 cannot be found.
Parameter name: columnName')