Ir para conteúdo

Arquivado

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

Julierme Felix

[Resolvido] Datagrid como referencia cruzada

Recommended Posts

Pessoal , preciso visualizar os dados em um datagrid mas que apareçam como uma tabela de referencia cruzada, estou usando BD em access.

 

hoje visualizado os dados assim

 

nome evento data

 

fulano_a 1ºp 01/05/2008

 

fulano_a 2ºP 01/04/2009

 

fulano_a 3ºp 01/05/2010

 

gostaria que ficasse assim

 

nome 1ºP 2ºP 3ºP

 

fulano_a 01/05/2008 01/04/2009 01/05/2000

 

 

 

não estou conseguindo gerar o datagrid dessa forma.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Pessoal,

Consegui resolver usando a função abaixo (achei a função na net)

Function GetInversedDataTable(ByVal table As DataTable, ByVal columnX As String, ByVal columnY As String, ByVal columnZ As String, ByVal nullValue As String, ByVal sumValues As Boolean) As DataTable
   'Create a DataTable to Return
   Dim returnTable As New DataTable

   If columnX = "" Then
     columnX = table.Columns(0).ColumnName
   End If

   'Add a Column at the beginning of the table
   returnTable.Columns.Add(columnY)

   'Read all DISTINCT values from columnX Column in the provided DataTale
   'Dim List As String
   'Dim columnXValues = New ListItem
   Dim columnXValues As New ArrayList

   For Each dr As DataRow In table.Rows

     Dim columnXTemp As String = dr(columnX).ToString()
     If Not columnXValues.Contains(columnXTemp) Then
       'Read each row value, if it's different from others provided, add to the list of values and creates a new Column with its value.
       columnXValues.Add(columnXTemp)
       returnTable.Columns.Add(columnXTemp)
     End If
   Next

   'Verify if Y and Z Axis columns re provided
   If columnY <> "" AndAlso columnZ <> "" Then
     'Read DISTINCT Values for Y Axis Column
     Dim columnYValues As New ArrayList
     'Dim columnYValues = New ListItem 'As New List(Of String)()

     For Each dr As DataRow In table.Rows
       If Not columnYValues.Contains(dr(columnY).ToString()) Then
         columnYValues.Add(dr(columnY).ToString())
       End If
     Next

     'Loop all Column Y Distinct Value
     For Each columnYValue As String In columnYValues
       'Creates a new Row
       Dim drReturn As DataRow = returnTable.NewRow()
       drReturn(0) = columnYValue
       'foreach column Y value, The rows are selected distincted
       Dim rows As DataRow() = table.[select]((columnY & "='") + columnYValue & "'")

       'Read each row to fill the DataTable
       For Each dr As DataRow In rows
         Dim rowColumnTitle As String = dr(columnX).ToString()

         'Read each column to fill the DataTable
         For Each dc As DataColumn In returnTable.Columns
           If dc.ColumnName = rowColumnTitle Then
             'If Sum of Values is True it try to perform a Sum
             'If sum is not possible due to value types, the value displayed is the last one read
             If sumValues Then
               Try
                 drReturn(rowColumnTitle) = Convert.ToDecimal(drReturn(rowColumnTitle)) + Convert.ToDecimal(dr(columnZ))
               Catch
                 drReturn(rowColumnTitle) = dr(columnZ)
               End Try
             Else
               drReturn(rowColumnTitle) = dr(columnZ)

             End If
           End If
         Next
       Next

       returnTable.Rows.Add(drReturn)

     Next
   Else
     Throw New Exception("The columns to perform inversion are not provided")
   End If

   'if a nullValue is provided, fill the datable with it
   If nullValue <> "" Then
     For Each dr As DataRow In returnTable.Rows
       For Each dc As DataColumn In returnTable.Columns
         If dr(dc.ColumnName).ToString() = "" Then
           dr(dc.ColumnName) = nullValue
         End If
       Next
     Next
   End If

   Return returnTable
 End Function

 

 

 

E para montar o datagrid usei o codigo abaixo

Public Sub carregardata()
   Dim dat1 As OleDbDataAdapter
   cn.Open()
   Dim myCommLin As OleDbCommand = cn.CreateCommand()
   Dim myCB As String = "SELECT * FROM COLATINA where NOM_IGREJA=@dep"
   myCommLin.CommandText = myCB
   myCommLin.Parameters.Add("@dep", OleDbType.VarChar).Value = cboigreja.SelectedValue
   dat1 = New OleDbDataAdapter(myCommLin)
   Dim DS1 = New DataSet
   DAT.SelectCommand = myCommLin
   DAT.Fill(DS1, "COLATINA")
   Dim dt2 As DataTable = New DataTable
   dt = DS1.Tables(0)
   dt2 = GetInversedDataTable(dt, "DSC_CLASSE", "NOM_MEMBRO", "EVENTO", "", True)
   cn.Close()
   DAT = Nothing
   cn = Nothing
   DataGrid1.DataSource = dt2
   DataGrid1.DataBind()
 End Sub

 

Mas agora Preciso formatar esse campo "EVENTO" no formato de data dd/mm/yyyy pois esta trazendo no formato mm/dd/yyyy e também com horas.

 

Tentei fazer assim nessa linha: dt2 = GetInversedDataTable(dt, "DSC_CLASSE", "NOM_MEMBRO", Format("EVENTO","dd/mm/yyyy"), "", True)não da certo pois diz que não tem a coluna dd/mm/yyyy

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.