lcfjoertoft 0 Light Poster

Hi all

I have "borrowed" a function from the net, and need som help in modifying it to do what I want.

Public Function CrossTab(ByVal dtS As DataTable, ByVal leftColumn As String, ByVal topField As String, ByVal dataValue As String, Optional ByVal pFix As String = "") As DataTable
        If dtS Is Nothing Then
            Return Nothing
        End If

        Dim dtOut As New DataTable
        Dim dtRowTitle As New DataTable
        Dim dtColHeader As New DataTable

        dtRowTitle = dtS.DefaultView.ToTable(True, dtS.Columns(leftColumn).ColumnName)
        dtColHeader = dtS.DefaultView.ToTable(True, dtS.Columns(topField).ColumnName)

        Dim dColx As New DataColumn
        dColx.ColumnName = leftColumn
        dColx.Caption = leftColumn
        dtOut.Columns.Add(dColx)

        For Each drow As DataRow In dtColHeader.Rows
            Dim dCol As New DataColumn
            dCol.ColumnName = pFix & drow.Item(topField).ToString.Trim
            dtOut.Columns.Add(dCol)
        Next

        Dim drowx As DataRow
        For Each drow As DataRow In dtRowTitle.Rows
            drowx = dtOut.NewRow()
            drowx.Item(0) = drow.Item(leftColumn)
            dtOut.Rows.Add(drowx)
        Next

        Dim xVal As Int32 = 0
        Dim yVal As Int32 = 0

        For Each mRow As DataRow In dtS.Rows
            Dim xRowVal As String = mRow.Item(leftColumn).ToString
            Dim dataVal As String = mRow.Item(dataValue).ToString
            Dim yColVal As String = mRow.Item(topField).ToString.Trim

            For Each nRow As DataRow In dtOut.Rows
                If xRowVal = nRow.Item(0).ToString Then
                    For xVal = 0 To nRow.Table.Columns.Count() - 1
                        If nRow.Table.Columns(xVal).ColumnName = pFix & yColVal Then
                            Dim rIndex As Int32 = dtOut.Rows.IndexOf(nRow)
                            dtOut.Rows(rIndex).Item(xVal) = dataVal
                            Exit For
                        End If
                    Next
                    Exit For
                End If
            Next
        Next

        dtOut.DefaultView.Sort = dtOut.Columns(0).ColumnName
        DataGridView1.DataSource = dtOut
        Return dtOut

    End Function

On row 10 (in the above code) I need to pass more than just one column. I need to pass inn 5 columns. Can anyone lend a hand?