-
Jun 21st, 2016, 08:08 PM
#1
Thread Starter
Addicted Member
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.
-
Jun 21st, 2016, 08:55 PM
#2
Re: vb.net: How to show the content of datagrideview vertically
What is the actual table structure?
example,
Table Name: StudentGrades
Fields: StundentId, Grade
-
Jun 21st, 2016, 09:56 PM
#3
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.
-
Jun 22nd, 2016, 09:42 AM
#4
Thread Starter
Addicted Member
Re: vb.net: How to show the content of datagrideview vertically
Originally Posted by wes4dbt
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]
-
Jun 22nd, 2016, 12:09 PM
#5
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:
SELECT [Subject], Grade FROM StudentReportCard UNPIVOT (Grade FOR [Subject] IN (English, Math, Literature, Chemistry)) AS U_SubjectGrade 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
-
Jun 23rd, 2016, 04:07 PM
#6
Thread Starter
Addicted Member
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
-
Jun 30th, 2016, 06:13 AM
#7
Thread Starter
Addicted Member
Re: vb.net: How to show the content of datagrideview vertically
Originally Posted by KGComputers
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:
SELECT [Subject],
Grade
FROM StudentReportCard
UNPIVOT (Grade FOR [Subject] IN (English, Math, Literature, Chemistry)) AS U_SubjectGrade
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
-
Jun 30th, 2016, 10:34 AM
#8
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
-
Jun 30th, 2016, 12:45 PM
#9
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
-
Jun 30th, 2016, 12:58 PM
#10
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.
-
Jun 30th, 2016, 01:09 PM
#11
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
-
Jun 30th, 2016, 01:14 PM
#12
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
-
Jun 30th, 2016, 01:30 PM
#13
Re: vb.net: How to show the content of datagrideview vertically
Originally Posted by kaliman79912
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|