SortCompare事件不会触发DataGridView动态添加到TabControl

时间:2016-07-04 16:53:07

标签: vb.net sorting datagridview event-handling

使用数据表作为datagridview的数据源可以很好地工作,但是,在对列进行排序时,我无法触发Dgv_SortCompare事件。主要问题是dgv值中(纯粹)数字列中的数值被排序为文本,例如, 1211.6小于89.7

In Button1:

Dim datagridview1 As New DataGridView
datagridview1.AutoSize = True
datagridview1.AutoResizeRows()
datagridview1.AutoResizeColumns()
datagridview1.ClearSelection()
DoubleBuffered(datagridview1, True)

datagridview1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
Dim dgvColumnHeaderStyle As New DataGridViewCellStyle()
dgvColumnHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
datagridview1.ColumnHeadersDefaultCellStyle = dgvColumnHeaderStyle
datagridview1.AllowUserToAddRows = False
datagridview1.ScrollBars = ScrollBars.Both
datagridview1.Dock = DockStyle.Fill

datagridview1.Refresh()
datagridview1.VirtualMode = False

Dim dgv As New DGVCREATE(datagridview1, dataarray, columnheaders, rowheaders, InputFeatureNames, InputObjectNames)

TabControl2.SelectedTab = TabControl2.TabPages.Item(0)
TabControl2.TabPages(0).Controls.Clear()
AddHandler() datagridview1.SortCompare, AddressOf dgv_SortCompare
TabControl2.TabPages(0).Controls.Add(datagridview1)
AddHandler CType(TabControl2.TabPages(0).Controls(0), DataGridView).SortCompare, AddressOf Me.dgv_SortCompare



Public Class DGVCREATE

    Sub New(ByRef dgv As DataGridView, ByVal dataarray(,) As Object, ByVal columnheaders() As String, ByVal rowheaders() As String, ByVal FieldNames() As String, ByVal RowNames() As String)

        dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing

        ' Create the output table.
        GetResultsTable(rxdataarray, columnheaders, rowheaders, FieldNames, RowNames)

        'new trial code
        dgv.AutoGenerateColumns = False

        dgv.DataSource = Form1.MainDataTable
        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

        'Set Column FillWeight in very large DGVs in order to prevent exception related to “Sum of FillWeight exceeds 65000”

        For i As Integer = 0 To Form1.MainDataTable.Columns.Count - 1
            Dim Column As New DataGridViewTextBoxColumn
            Column.Name = Form1.MainDataTable.Columns(i).ColumnName
            Column.DataPropertyName = Form1.MainDataTable.Columns(i).ColumnName
            Column.HeaderText = Form1.MainDataTable.Columns(i).ColumnName
            Column.FillWeight = 20
            Column.MinimumWidth = 20
            dgv.Columns.Add(Column)
        Next i

        For i As Integer = 0 To dgv.Columns.Count - 1
            If InputFeatureType(i + 1) = 1 Then
                dgv.Columns(i).ValueType = GetType(Int64)
                dgv.Columns(i).SortMode = DataGridViewColumnSortMode.Automatic
            End If
            If InputFeatureType(i + 1) = 2 Then
                dgv.Columns(i).ValueType = GetType(Double)
                dgv.Columns(i).SortMode = DataGridViewColumnSortMode.Automatic
            End If
            If InputFeatureType(i + 1) = 3 Then
                dgv.Columns(i).ValueType = GetType(Double)
                dgv.Columns(i).SortMode = DataGridViewColumnSortMode.Automatic
            End If
            If InputFeatureType(i + 1) = 4 Then
                dgv.Columns(i).ValueType = GetType(String)
                dgv.Columns(i).SortMode = DataGridViewColumnSortMode.Automatic
            End If
        Next i

        dgv.AutoSize = True
    End Sub

    Public Sub GetResultsTable(ByVal dataarray(,) As Object, ByVal columnheaders() As String, ByVal rowheaders() As String, ByVal fieldnames() As String, ByVal rownames() As String)
        Form1.MainDataTable.Clear()
        Form1.MainDataTable.Rows.Clear()
        Form1.MainDataTable.Columns.Clear()
        ' Loop through all process names.
        For j As Integer = 0 To UBound(columnheaders) - 1
            ' The current process name.
            ' Add the program name to our columns.
            Form1.MainDataTable.Columns.Add(fieldnames(j + 1))
            'Form1.MainDataTable.Columns.Add(fieldnames(j + 1))
            ' Keep adding rows until we have enough.
            Do While Form1.MainDataTable.Rows.Count < UBound(rowheaders)
                Form1.MainDataTable.Rows.Add()
            Loop
            ' Add each item to the cells in the column.
            For i As Integer = 0 To UBound(rowheaders) - 1
                Form1.MainDataTable.Rows(i)(j) = dataarray(i, j)
            Next i
        Next j
    End Sub
End Class

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

那里有很多不需要的代码。首先,您希望避免装箱数据As Object,因为它可以防止像DGV这样的东西看到它的类型。 DataTable完全能够存储键入的数据。如果必须,您可以创建一个类似DTO的类:

Public Class DataItem
    Public Property Able As Int64
    Public Property Baker As Double
    Public Property Charlie As Double
    Public Property Delta As String
End Class

由于DataTable可以包含多种类型,因此请使用它来代替DataArray,并避免数据在那里进行跳转:

Private dtData As DataTable
...
' create it somewhere
dtData = New DataTable
dtData.Columns.Add("Able", GetType(Int64))
dtData.Columns.Add("Baker", GetType(Double))
dtData.Columns.Add("Chanrlie", GetType(Double))   ' aka Charlie
dtData.Columns.Add("Delta", GetType(String))

填写数据:

For n As Int32 = 0 To 999
    dr = dtData.NewRow

    dr(0) = RNG.Next()        ' random value
    dr(1) = RNG.NextDouble()  ' random 0.00 to 1.0
    dr(2) = RNG.Next(-19, 20) + RNG.NextDouble()  ' -19.xx to +19.xx
    dr(3) = RD.GetNames(2, 35)  ' 2 random names
    ' add new row:
    dtData.Rows.Add(dr)
Next

当它被填满时,让DGV为你创建列 - 它将从源读取数据类型等:

dgv1.DataSource = dtData
For Each dc As DataGridViewColumn In dgv1.Columns
    Console.WriteLine(dc.ValueType)
Next

DGV会看到类型,并会正确排序任何类型:

  

System.Int64
  System.Double
  System.Double
  System.String

enter image description here