Create a Pivot Table from a DataTable

后端 未结 4 1920
耶瑟儿~
耶瑟儿~ 2021-02-07 23:53

I am using C# winforms to create an application that needs to turn a datatable into a pivot table. I have the pivot table working fine from a SQL end, but creating it from a da

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-08 00:50

    Maybe this will help you. It is in vb.net.

    Public Function pivot_datatable(ByVal datatable_source As DataTable, ByVal datacolumn_rows As DataColumn(), ByVal datacolumn_columns As DataColumn, ByVal datacolumn_value As DataColumn) As DataTable
        Dim temp_datacolumn As DataColumn
        Dim current_datarow As DataRow
        Dim datarow_destination As DataRow = Nothing
        Dim current_column_name As String = ""
        Dim primary_key() As DataColumn = New DataColumn() {}
        Dim key_columns() As Object
        Dim newOrdinal As Integer
        Dim i As Integer
        Dim sort_string As String = ""
    
        Try
            pivot_datatable = New DataTable()
    
            For Each temp_datacolumn In datatable_source.Columns
                If temp_datacolumn.Ordinal <> datacolumn_columns.Ordinal AndAlso temp_datacolumn.Ordinal <> datacolumn_value.Ordinal Then
                    array_insert(primary_key, pivot_datatable.Columns.Add(temp_datacolumn.ColumnName, temp_datacolumn.DataType))
                    sort_string &= temp_datacolumn.ColumnName & " ASC, "
                End If
            Next
            pivot_datatable.PrimaryKey = primary_key
    
            For Each current_datarow In datatable_source.Rows ' Main Process to add values to pivot table
                current_column_name = current_datarow(datacolumn_columns.Ordinal).ToString
                If Not pivot_datatable.Columns.Contains(current_column_name) Then ' Column is new
                    temp_datacolumn = pivot_datatable.Columns.Add(current_column_name, datacolumn_value.DataType)
                    newOrdinal = temp_datacolumn.Ordinal
                    For i = newOrdinal - 1 To datatable_source.Columns.Count - 2 Step -1
                        If temp_datacolumn.ColumnName.CompareTo(pivot_datatable.Columns(i).ColumnName) < 0 Then
                            newOrdinal = i
                        End If
                    Next
                    temp_datacolumn.SetOrdinal(newOrdinal)
                End If
    
                key_columns = New Object() {}
                For Each data_column As DataColumn In datacolumn_rows
                    array_insert(key_columns, current_datarow(data_column.Ordinal).ToString)
                Next data_column
                datarow_destination = pivot_datatable.Rows.Find(key_columns)
                If datarow_destination Is Nothing Then ' New Row
                    datarow_destination = pivot_datatable.NewRow()
                    For Each temp_datacolumn In datatable_source.Columns
                        If temp_datacolumn.Ordinal <> datacolumn_columns.Ordinal AndAlso temp_datacolumn.Ordinal <> datacolumn_value.Ordinal Then
                            datarow_destination(temp_datacolumn.ColumnName) = current_datarow(temp_datacolumn.ColumnName)
                        End If
                    Next
                    pivot_datatable.Rows.Add(datarow_destination)
                End If
                datarow_destination(current_column_name) = current_datarow(datacolumn_value.Ordinal)
            Next
    
            Return sort_datatable(pivot_datatable, sort_string.Substring(0, sort_string.Length - 2))
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    

提交回复
热议问题