Link to home
Start Free TrialLog in
Avatar of jjliu4492
jjliu4492

asked on

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I don't understand your question...what is it that you don't understand?  Are you looking for formatting advice?
Avatar of jjliu4492
jjliu4492

ASKER

I want the data from the database to look like the above screenshot when I display it in a datagridview control.
Did you define columns for the DataGridView?  Did you bind the data to the DataGridView?  What are you having difficulty with?
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

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?
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);
If you are working with SQL Server, on the back-end, then you could create a stored procedure to create a pivot.
How do I create a pivot using a stored procedure?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial