DatagridView Columns SQL server VB

0

Good Night. Excuse me, I'd like you to help me, I'm doing a datagridview that has these columns that I put:

  • ID
  • Full Names
  • Full Surnames

and in my database I have two tables created:

The Students table that has these fields:

  • IdAlumno
  • Names
  • Last name
  • Age
  • IdCourses
  • Note

The Courses table that has these Fields:

  • IdCourses
  • Courses

as the attached image:

but I in my datagrid want you to come out with those columns: so the records come out as a column only in the courses and below your note like this:

This is my code I would like you to help me please:

 Private Sub btnMostrar_Click(sender As Object, e As EventArgs) Handles btnMostrar.Click
        Dim SqlDataAdapter As New SqlDataAdapter("select*from alumno", SqlConnection)
        Dim DataTable As New DataTable()
        SqlDataAdapter.Fill(DataTable)
        For Each DataRow As String In DataTable.Rows
            Dim n As Integer = (DataGridView1.Rows.Add)

        Next

    End Sub
    
asked by PieroDev 21.02.2017 в 04:45
source

1 answer

1

I recommend you from the SQL do a pivot , what which consists of converting the Rows into Columns or vice versa (unpivot).

It would be like this:

SELECT a.idalumno, a.nombre, a.apellido, a.edad, a.idcursos, a.nota, c.cursos
FROM alumnos a
INNER JOIN cursos c ON a.idcursos = c.idcursos
PIVOT (SUM(a.nota) FOR c.cursos IN ([Matematica], [Comunicacion], [Religion], [Fisica]))

I hope this helps you and gives you a guide.

    
answered by 21.02.2017 в 15:50