Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Ilano

Como usar esta função (Function Pivot)?

Recommended Posts

Olá pessoal,

 

Encontrei na net esta função, mas não sei como usá-la. Alguém pode me dar uma ajudinha?

 

Public Shared Function Pivot(ByVal dataValues As Data.IDataReader, ByVal keyColumn As String, ByVal pivotNameColumn As String, ByVal pivotValueColumn As String) As Data.DataTable

Dim tmp As New Data.DataTable()

Dim r As Data.DataRow

Dim LastKey As String = "//dummy//"

Dim i As Integer, pValIndex As Integer, pNameIndex As Integer

Dim s As String

Dim FirstRow As Boolean = True

' Add non-pivot columns to the data table:

pValIndex = dataValues.GetOrdinal(pivotValueColumn)

pNameIndex = dataValues.GetOrdinal(pivotNameColumn)

 

For i = 0 To dataValues.FieldCount - 1

If i <> pValIndex AndAlso i <> pNameIndex Then

tmp.Columns.Add(dataValues.GetName(i), dataValues.GetFieldType(i))

End If

Next

 

r = tmp.NewRow()

' now, fill up the table with the data:

While dataValues.Read()

' see if we need to start a new row

If dataValues(keyColumn).ToString() <> LastKey Then

' if this isn't the very first row, we need to add the last one to the table

If Not FirstRow Then

tmp.Rows.Add®

End If

r = tmp.NewRow()

FirstRow = False

' Add all non-pivot column values to the new row:

For i = 0 To dataValues.FieldCount - 3

r(i) = dataValues(tmp.Columns(i).ColumnName)

Next

LastKey = dataValues(keyColumn).ToString()

End If

' assign the pivot values to the proper column; add new columns if needed:

s = dataValues(pNameIndex).ToString()

If Not tmp.Columns.Contains(s) Then

tmp.Columns.Add(s, dataValues.GetFieldType(pValIndex))

End If

r(s) = dataValues(pValIndex)

End While

' add that final row to the datatable:

tmp.Rows.Add®

' Close the DataReader

dataValues.Close()

' and that's it!

Return tmp

End Function

Grato,

 

Ilano.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.