0

我正在尝试将 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')

4

1 回答 1

0

如果您尝试更改线路怎么样:

    Dim rwIdx = 0
    For Each selectedrows As DataGridViewRow In DGV.Rows
        Dim myDataGridViewRow As New DataGridViewRow
        myDataGridViewRow = DGV.Rows(rwIdx)
        rwIdx = rwIdx + 1
        selectedrows.Tag = myDataGridViewRow
    Next

上面只会引用同一行,不会克隆数据,下面,我会将 selectedrows 复制到另一个 datagridview:

    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
    Dim DataGridViewRow As New DataGridViewRow
    Dim myRowIdx As Integer = 0
    For Each selectedrows As DataGridViewRow In DGV.Rows
        DataGridViewRow = New DataGridViewRow
        Dim dgv2 As New DataGridView
        For Each col As DataGridViewColumn In DGV.Columns
            dgv2.Columns.Add(DirectCast(col.Clone, DataGridViewColumn))
        Next
        For Each Cell As DataGridViewCell In selectedrows.Cells
            dgv2.Rows(0).Cells(Cell.ColumnIndex).Value = Cell.Value
        Next
        selectedrows.Tag = dgv2.Rows(0)
    Next
    MessageBox.Show(DGV.Rows(0).Cells(0).Value.ToString)                                       ''Works (Output: 1)
    MessageBox.Show(DirectCast(DGV.Rows(0).Tag, DataGridViewRow).Cells(0).Value.ToString)      ''Works (Output: 1)

    DGV.Rows(0).Cells(0).Value = "Change"
    MessageBox.Show(DGV.Rows(0).Cells(0).Value.ToString)                                       ''Works (Output: Change)
    MessageBox.Show(DirectCast(DGV.Rows(0).Tag, DataGridViewRow).Cells(0).Value.ToString)      ''Works (Output: 1)


    MessageBox.Show(DGV.Rows(0).Cells("No1").Value.ToString)                                   ''Works (Output: Change) 
    MessageBox.Show(DirectCast(DGV.Rows(0).Tag, DataGridViewRow).Cells("No1").Value.ToString)  ''Works (Output: 1)
于 2019-09-16T00:49:19.943 回答