嘿我在使用Allowusertoordercolumns属性更改Displayindex时尝试更改dvg的Column索引但是当我移动列时我无法更改列索引。
我收到一个错误,指出列索引是只读的..无论如何都在这周围。我到目前为止已经得到了
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
按钮
据我所知,这与我刚才想要的映射按钮一样
Dim nbColumnsToTransfer As Integer = DataGridView2.Columns.GetColumnCount(1)
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 - 2
Dim RNUM1 = (DataGridView1.Rows(c).Cells(indexes(0)).Value)
Dim RNUM2 = (DataGridView1.Rows(c).Cells(indexes(1)).Value)
If chkContactSplitReq.Checked = True Then
If IsDBNull(DataGridView1.Rows(c).Cells(indexes(2)).Value) Then
Dim RNUM3 As String = ""
Dim RNUM4 As String = ""
Dim RNUM5 = (DataGridView1.Rows(c).Cells(indexes(5)).Value)
Dim RNUM6 = (DataGridView1.Rows(c).Cells(indexes(6)).Value)
Dim RNUM7 = (DataGridView1.Rows(c).Cells(indexes(7)).Value)
DataGridView2.Rows.Add(RNUM1, RNUM2, RNUM3, RNUM4, RNUM5, RNUM6, RNUM7)
Else
Dim tempString As String = (DataGridView1.Rows(c).Cells(indexes(2)).Value)
Dim Split() As String = tempString.Split(" "c)
Dim RNUM3 As String = Split(0)
If Split.Length > 1 Then
Dim RNUM4 As String = Split(1)
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(RNUM1, RNUM2, RNUM3, RNUM4, RNUM5, RNUM6, RNUM7)
Else
Dim RNUM4 As String = ""
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(RNUM1, RNUM2, RNUM3, RNUM4, RNUM5, RNUM6, RNUM7)
End If
End If
Else
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(RNUM1, RNUM2, RNUM3, RNUM4, RNUM5, RNUM6, RNUM7)
End If
答案 0 :(得分:1)
如果我很清楚你想做什么,你可以这样做,将第一个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