How to display data from SQL Server 2008 onto a datagridview in different format

I have the following data, which I pull from a SQL Server 2008 database.

ProjectHourID      TaskID WeekEndingDate      ActualHours BudgetHours RemainingHours TaskName
2                         1           9/30/2011                10                      50                      40                       test 1
3                         2           9/30/2011                15                    100                      75                       test 2
4                         1           9/23/2011                  5                      45                      40                       test 1
5                         2           9/23/2011                10                    100                      90                       test 2

I want to display the data above in a VS2010 windows forms application, using a datagridviewcontrol. I want to display the data in the following format on the datagridview (see screenshot). How would I do this?

Right now I am using the following code to populate the datagridview:

                OperationsDataContext opDC = new OperationsDataContext();

                dgTime.DataSource = opDC.Tasks_GetByProjectIDAndWeekEndingDate(Convert.ToInt32(cboTimeProject.SelectedValue), dtWeekEnding.Value);

                dgTime.AutoResizeColumns();

DatagridviewFormat.png
jjliu4492Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
I don't understand your question...what is it that you don't understand?  Are you looking for formatting advice?
0
jjliu4492Author Commented:
I want the data from the database to look like the above screenshot when I display it in a datagridview control.
0
Bob LearnedCommented:
Did you define columns for the DataGridView?  Did you bind the data to the DataGridView?  What are you having difficulty with?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

jjliu4492Author Commented:
I want the data that is pulled from the database to be displayed in a certain format within the datagridview control.

ProjectHourID      TaskID WeekEndingDate      ActualHours BudgetHours RemainingHours TaskName
2                         1           9/30/2011                10                      50                      40                       test 1
3                         2           9/30/2011                15                    100                      75                       test 2
4                         1           9/23/2011                  5                      45                      40                       test 1
5                         2           9/23/2011                10                    100                      90                       test 2

0
jjliu4492Author Commented:
0
Bob LearnedCommented:
OK, it seems like you want to create a cross-tab view of that data.  I would suggest doing that work on the back end.  What kind of data source are you working with?
0
jjliu4492Author Commented:
I am using linq to sql to pull the data from the database.

OperationsDataContext opDC = new OperationsDataContext();
                dgTime.DataSource = null;

                dgTime.DataSource = opDC.ProjectHours_GetByProjectIDAndWeekEndingDate(Convert.ToInt32(cboTimeProject.SelectedValue), dtWeekEnding.Value);
0
Bob LearnedCommented:
If you are working with SQL Server, on the back-end, then you could create a stored procedure to create a pivot.
0
jjliu4492Author Commented:
How do I create a pivot using a stored procedure?
0
Bob LearnedCommented:
The PIVOT keyword was introduced in 2005, so if you have an older version, I wouldn't know what the equivalence would be.

Using PIVOT and UNPIVOT
http://msdn.microsoft.com/en-us/library/ms177410(v=SQL.90).aspx

SQL SERVER – PIVOT and UNPIVOT Table Examples
http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/
SELECT PRODUCT, FRED, KATE
FROM (
SELECT CUST, PRODUCT, QTY
FROM Product) up
PIVOT (SUM(QTY) FOR CUST IN (FRED, KATE)) AS pvt
ORDER BY PRODUCT

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.