Vb.net ColumnIndex和DisplayIndex数据网格视图

Vb.net ColumnIndex和DisplayIndex数据网格视图,vb.net,datagridview,mapping,Vb.net,Datagridview,Mapping,嘿,当我使用Allowusertoordercolumns属性更改Displayindex时,我试图更改dvg的列索引,但是当我移动列时,我无法让它更改列索引 我得到一个错误,说列索引是只读的。。这件事有什么不妥吗。这是我到目前为止所得到的 Private Sub DataGridView1_ColumnDisplayIndexChanged(sender As Object, e As DataGridViewColumnEventArgs) _ Handles DataGridView1.

嘿,当我使用Allowusertoordercolumns属性更改Displayindex时,我试图更改dvg的列索引,但是当我移动列时,我无法让它更改列索引

我得到一个错误,说列索引是只读的。。这件事有什么不妥吗。这是我到目前为止所得到的

Private Sub DataGridView1_ColumnDisplayIndexChanged(sender As Object, e As DataGridViewColumnEventArgs) _
 Handles DataGridView1.ColumnDisplayIndexChanged

    Dim messageBoxVB As New System.Text.StringBuilder()
    messageBoxVB.AppendFormat("{0} = {1}", "Column", e.Column)
    messageBoxVB.AppendLine()
    '   MessageBox.Show(messageBoxVB.ToString(), "ColumnDisplayIndexChanged Event")

    If DataGridView1.Columns(e.Column.Name).Index <> e.Column.DisplayIndex Then
        DataGridView1.Columns(e.Column.Name).Index = (e.Column.DisplayIndex)
    End If
    'DataGridView1 = DataGridView1
End Sub

如果我很了解您想要做什么,您可以这样做,将数据从第一个DataGridView加载到第二个DataGridView:

DataGridView2.Rows.Clear()
For i As Integer = 0 to DataGridView1.Rows.Count - 1
    Datagridview2.Rows.Add(Datagridview1.Rows(i).Cells("FirstColumn").Value, Datagridview1.Rows(i).Cells("SecondColumn").Value, Datagridview1.Rows(i).Cells("ThirdColumn").Value)
    ' or
    ' Datagridview2.Rows.Add(Datagridview1.Rows(i).Cells(0).Value, Datagridview1.Rows(i).Cells(1).Value, Datagridview1.Rows(i).Cells(2).Value)
Next
这样,您就不必对第二个DataGridView的列重新排序,除非您愿意。无论如何,有一件事是肯定的,DataGridViewColumn的index属性是只读的,因此您不能按您想要的方式执行

编辑

如果您不介意使用一点Linq,您可以这样做。您也可以在没有Linq的情况下执行此操作,但这里有一个想法:

Dim NbOfSecondGridviewColumns As Integer = 3    
Dim indexes As List(Of Integer) = (From column As DataGridViewColumn In DataGridView1.Columns.Cast(Of DataGridViewColumn)() _
                                   Take NbOfSecondGridviewColumns _
                                   Order By column.DisplayIndex _
                                   Select column.Index).ToList()

For i As Integer = 0 to DataGridView1.Rows.Count - 1
    Datagridview2.Rows.Add(Datagridview1.Rows(i).Cells(indexes(0)).Value, Datagridview1.Rows(i).Cells(indexes(1)).Value, Datagridview1.Rows(i).Cells(indexes(2)).Value)
Next
编辑2

以下是我将在您的按钮代码中执行的操作:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim nbColumnsToTransfer As Integer = DataGridView2.Columns.GetColumnCount(1)

    ' Get the indexes of the first [nbColumnsToTransfer] columns, ordered by their 
    ' display index, because your columns to be transferred are the first displayed
    Dim indexes As List(Of Integer) = (From column As DataGridViewColumn In DataGridView1.Columns.Cast(Of DataGridViewColumn)() _
                                       Take nbColumnsToTransfer _
                                       Order By column.DisplayIndex _
                                       Select column.Index).ToList()

    For c As Integer = 0 To DataGridView1.Rows.Count - 1

        ' Dim RNUM1 = (DataGridView1.Rows(c).Cells(0).Value) => this won't work 
        ' because cells takes a column index value or column name in parameter. Don't 
        ' forget your columns displayed index ≠ your columns index,
        ' that's why I used the linq query above
        Dim RNUM1 = (DataGridView1.Rows(c).Cells(indexes(0)).Value)
        Dim RNUM2 = (DataGridView1.Rows(c).Cells(indexes(1)).Value)
        Dim RNUM3 = (DataGridView1.Rows(c).Cells(indexes(2)).Value)
        Dim RNUM4 = (DataGridView1.Rows(c).Cells(indexes(3)).Value)
        Dim RNUM5 = (DataGridView1.Rows(c).Cells(indexes(4)).Value)
        Dim RNUM6 = (DataGridView1.Rows(c).Cells(indexes(5)).Value)
        Dim RNUM7 = (DataGridView1.Rows(c).Cells(indexes(6)).Value)

        DataGridView2.Rows.Add(RNUM2, RNUM4, RNUM3, RNUM3, RNUM6, RNUM5, RNUM4)
    Next
End Sub

不,您不能更改DataGridView的列索引。你为什么要这么做?您可以找到有趣的注释:“此属性的值不一定对应于集合中带区的当前可视位置。例如,如果用户在运行时对DataGridView中的列重新排序(假设AllowUserToOrderColumns属性设置为true),每列的Index属性值不会更改。相反,列DisplayIndex值会更改。但是,排序行会更改其索引值。“请参阅,我正在尝试映射两个不同的dgv,我从excel spreed工作表加载数据,然后更改列的位置以匹配第二个datagridview的位置,然后传输数据。由于新添加的表每次都会不同,并且可能会有不同的标题,例如“Company”和“CompanyName”,我不确定如何执行此操作,我不确定我是否理解。第二个DataGridView是第一个的克隆吗?是某些列的克隆吗。一些来自第一个dgv的数据不应该被丢弃。看到标题名称会随着每个电子表格的加载而发生轻微变化,比如说“公司名称”。。。一个不同的电子表格可能只有“公司”,所以我不能用标题名。你是这么建议的吗?是否有其他方法可以解决这个问题,然后通过列索引来解决。例如,您可以轻松地执行
单元格(0)
,而不是
单元格(“第一列”)
。如果还有什么我没有考虑到的,不要犹豫告诉我。是的,我考虑过这一点,但他们也可以在不同的顺序加载,所以。。。这就是为什么我一直使用Allowusertoordercolumns作为true来对它们进行排序,并尝试以某种方式使用displayindex来更改列索引。实际上,此查询所做的是获取第一个显示列的列索引。我在这里的逻辑是对第一个datagridview列重新排序,以便其第一个显示的列对应于第二个datagridview的列。如果这是您正在做的,请发布按钮的单击代码,以便我可以进一步帮助您。很抱歉,我犯了一个错误,在select子句之后,列实际上不再在范围内。在选择之前将订单移动一步。请参见编辑。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim nbColumnsToTransfer As Integer = DataGridView2.Columns.GetColumnCount(1)

    ' Get the indexes of the first [nbColumnsToTransfer] columns, ordered by their 
    ' display index, because your columns to be transferred are the first displayed
    Dim indexes As List(Of Integer) = (From column As DataGridViewColumn In DataGridView1.Columns.Cast(Of DataGridViewColumn)() _
                                       Take nbColumnsToTransfer _
                                       Order By column.DisplayIndex _
                                       Select column.Index).ToList()

    For c As Integer = 0 To DataGridView1.Rows.Count - 1

        ' Dim RNUM1 = (DataGridView1.Rows(c).Cells(0).Value) => this won't work 
        ' because cells takes a column index value or column name in parameter. Don't 
        ' forget your columns displayed index ≠ your columns index,
        ' that's why I used the linq query above
        Dim RNUM1 = (DataGridView1.Rows(c).Cells(indexes(0)).Value)
        Dim RNUM2 = (DataGridView1.Rows(c).Cells(indexes(1)).Value)
        Dim RNUM3 = (DataGridView1.Rows(c).Cells(indexes(2)).Value)
        Dim RNUM4 = (DataGridView1.Rows(c).Cells(indexes(3)).Value)
        Dim RNUM5 = (DataGridView1.Rows(c).Cells(indexes(4)).Value)
        Dim RNUM6 = (DataGridView1.Rows(c).Cells(indexes(5)).Value)
        Dim RNUM7 = (DataGridView1.Rows(c).Cells(indexes(6)).Value)

        DataGridView2.Rows.Add(RNUM2, RNUM4, RNUM3, RNUM3, RNUM6, RNUM5, RNUM4)
    Next
End Sub