我正在尝试将 DataGridViewRow 的精确副本存储到同一行的标签中,一切正常,结果应该是......除了当我尝试使用列 Name 检索值时(在最后一行代码),它告诉我列名不存在。但如果我使用索引,一切正常。
我曾尝试使用 CreateCells() 或 Clone() 方法来创建原始表的结构,但仍然无法正常工作。
我可以坚持使用索引。但我更愿意按列名获取值。那可能吗?
''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.
参数名称:columnName')