Results 1 to 13 of 13

Thread: vb.net: How to show the content of datagrideview vertically

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    204

    vb.net: How to show the content of datagrideview vertically

    Hi, I am displaying the contents of a dataset on a DataGridView but I do not want the columns to appear as the are on the database table

    Example(DataGridView)
    English, Math, Literature, Chemistry
    90 80 88 98


    I will like it to appear this way (DataGridView)
    Subject Grade
    English 90
    Math 80
    Literature 88
    Chemistry 98
    thanks
    Last edited by alobi; Jun 21st, 2016 at 08:13 PM.

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: vb.net: How to show the content of datagrideview vertically

    What is the actual table structure?

    example,
    Table Name: StudentGrades
    Fields: StundentId, Grade

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: vb.net: How to show the content of datagrideview vertically

    You can either populate the grid (or an alternate data source) manually or you can perform a pivot in your query to get the data in that format to begin with and then bind the DataTable to the grid.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    204

    Re: vb.net: How to show the content of datagrideview vertically

    Quote Originally Posted by wes4dbt View Post
    What is the actual table structure?

    example,
    Table Name: StudentGrades
    Fields: StundentId, Grade
    [QUOTE]Table Name: StudentReportCard
    Field Name; StudentId, StudentName, English, English, Math History, CRK, Biology
    I hope I answered your question correctly
    Thank you for your time.[QUOTE]

  5. #5
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: vb.net: How to show the content of datagrideview vertically

    Given your using SQL Server as your DB, you can use UNPIVOT to display the data in that format and then bind the DataTable object to the DataGridView as suggested.

    SQL Code:
    1. SELECT [Subject],
    2.            Grade
    3. FROM StudentReportCard
    4. UNPIVOT (Grade FOR [Subject] IN (English, Math, Literature, Chemistry)) AS U_SubjectGrade
    5. WHERE StudentId = 1

    Note: The above query shows the subjects with grades of Student with ID 1. You may add the additional subject names in the IN clause.
    Last edited by KGComputers; Jun 22nd, 2016 at 12:51 PM. Reason: Added note
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    204

    Re: vb.net: How to show the content of datagrideview vertically

    @kGCOMPUTERS, Thanks, I will apply what you said and get back with you
    AL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    204

    Re: vb.net: How to show the content of datagrideview vertically

    Quote Originally Posted by KGComputers View Post
    Given your using SQL Server as your DB, you can use UNPIVOT to display the data in that format and then bind the DataTable object to the DataGridView as suggested.

    SQL Code:
    1. SELECT [Subject],
    2.            Grade
    3. FROM StudentReportCard
    4. UNPIVOT (Grade FOR [Subject] IN (English, Math, Literature, Chemistry)) AS U_SubjectGrade
    5. WHERE StudentId = 1

    Note: The above query shows the subjects with grades of Student with ID 1. You may add the additional subject names in the IN clause.
    I am using Microsoft Access as the DB
    Thanks

  8. #8
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: vb.net: How to show the content of datagrideview vertically

    I haven't used MS Access for quite some time now. The workaround for Unpivot in Access is stated here: How to simulate UNPIVOT in Access 2010
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  9. #9
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: vb.net: How to show the content of datagrideview vertically

    going the long road...

    Code:
    Select 'English' AS Subject, English AS Grade FROM StudentReportCard 
    UNION Select 'Math' AS Subject, Math AS Grade FROM StudentReportCard 
    UNION Select 'Litarature' AS Subject, Litarature AS Grade FROM StudentReportCard 
    UNION Select 'History' AS Subject, History AS Grade FROM StudentReportCard
    WHERE StudentID = '000'
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  10. #10
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: vb.net: How to show the content of datagrideview vertically

    kali,

    I understand your post. But the link that KG provided has this example,
    Code:
    SELECT EmployeeID, "SKILL1" AS SkillID, SKILL1 AS Level_OF_Knowledge WHERE SKILL1 IS NOT NULL
    UNION ALL SELECT EmployeeID, "SKILL2" AS SkillID, SKILL2 AS Level_OF_Knowledge  WHERE SKILL2 IS NOT NULL
    UNION ALL SELECT EmployeeID, "SKILL3" AS SkillID, SKILL3 AS Level_OF_Knowledge  WHERE SKILL3 IS NOT NULL
    Nowhere in that SQL does it say what Table to Select from, no "From SomeTable". I don't underdstand how it works.

    I know this isn't my thread but maybe the OP is also confused.

  11. #11
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: vb.net: How to show the content of datagrideview vertically

    I had not seen that post but it is basically the same as mine, except as you point out, it does not specify the table where to get the data and it is necesary, otherwise the server can't guess what you want
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  12. #12
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: vb.net: How to show the content of datagrideview vertically

    Update, you need to specify the filter for each sub select:
    Code:
    Select 'English' AS Subject, English AS Grade FROM StudentReportCard  WHERE StudentID = 1
    UNION Select 'Math' AS Subject, Math AS Grade FROM StudentReportCard WHERE StudentID = 1
    UNION Select 'Litarature' AS Subject, Literature AS Grade FROM StudentReportCard WHERE StudentID = 1
    UNION Select 'History' AS Subject, History AS Grade FROM StudentReportCard WHERE StudentID = 1
    Or include the StudentID in the result

    Code:
    SELECT * FROM (SELECT StudentID, 'English' AS Subject, English AS Grade FROM StudentReportCard
    UNION Select StudentID, 'Math' AS Subject, Math AS Grade FROM StudentReportCard
    UNION Select StudentID, 'Litarature' AS Subject, Literature AS Grade FROM StudentReportCard
    UNION Select StudentID, 'History' AS Subject, History AS Grade FROM StudentReportCard) AS A
    WHERE StudentID = 1
    Last edited by kaliman79912; Jun 30th, 2016 at 01:17 PM.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  13. #13
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: vb.net: How to show the content of datagrideview vertically

    Quote Originally Posted by kaliman79912 View Post
    I had not seen that post but it is basically the same as mine, except as you point out, it does not specify the table where to get the data and it is necesary, otherwise the server can't guess what you want
    That's what I thought but the comments underneath said it worked. That's why I was confused.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width