Few members requested me to do the same for Winform using DataGridView. I have made a simple program with same functionality as my Webform example. Kindly refer both the articles to find more detail about project Scheduling.
Code Part
We will create a SCHED_Master table under the Database 'projectDB'. The following is the script to create a database, table and sample insert query. Run this script in your SQL Server. I have used SQL Server 2012.
SCHED_MASTER Table Field Name | Details |
| ProjectName | To insert the Name of each project |
| ProjectType | This is to define the category of the each project for example like if its urgent then its 1 and Important then its 2 and etc. |
| ProjectTypeName | This is to give the name of the project type as Urgent, Important and etc. |
| SCHED_ST_DT | The Schedule Start Date (This is important as we give each project Schedule Start Date. This is like a base start date for each project) |
| SCHED_ED_DT | The Schedule END Date (This is important as we give each project Schedule END Date. This is like a base End date for each project) |
| ACT_ST_DT | The Actual Start Date (This is important as we give each project Actual Start Date. This is a real time production start date) |
| ACT_ED_DT | The Actual End Date (This is important as we give each project Actual End Date. This is a real time production End date) |
Create Database and Table
- USE MASTER GO Check for the Database Exists.If the database is exist then
- drop
- and create new DB IF EXISTS
- (
- SELECT
- [name]
- FROM
- sys.databases
- WHERE
- [name] = 'projectDB'
- )
- DROP
- DATABASE projectDB GO CREATE DATABASE projectDB GO USE projectDB GO CREATE TABLE [dbo].[SCHED_Master]
- (
- [ID] [int] NOT NULL,
- [ProjectName] [varchar](100) NULL,
- [ProjectType] int NULL,
- [ProjectTypeName] [varchar](100) NULL,
- [SCHED_ST_DT] [datetime] NULL,
- [SCHED_ED_DT] [datetime] NULL,
- [ACT_ST_DT] [datetime] NULL,
- [ACT_ED_DT] [datetime] NULL,
- [status] int null PRIMARY KEY CLUSTERED ([ID] ASC) WITH (
- PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
- IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
- ALLOW_PAGE_LOCKS = ON
- ) ON [PRIMARY]
- ) ON [PRIMARY]
delete from SCHED_Master
- INSERT INTO [dbo].SCHED_Master
- (
- [ID], [ProjectName], [ProjectType],
- [ProjectTypeName], [SCHED_ST_DT],
- [SCHED_ED_DT], [ACT_ST_DT], [ACT_ED_DT],
- [status]
- )
- VALUES
- (
- 1001, 'Project-1', 1, 'Urgent', '2015-06-01 00:00:00.000',
- '2015-09-02 00:00:00.000', '2015-06-22 00:00:00.000',
- '2015-08-26 00:00:00.000', 1
- )
- INSERT INTO [dbo].SCHED_Master
- (
- [ID], [ProjectName], [ProjectType],
- [ProjectTypeName], [SCHED_ST_DT],
- [SCHED_ED_DT], [ACT_ST_DT], [ACT_ED_DT],
- [status]
- )
- VALUES
- (
- 1002, 'Project-1', 2, 'Important',
- '2015-06-12 00:00:00.000', '2015-8-02 00:00:00.000',
- '2015-06-19 00:00:00.000', '2015-8-29 00:00:00.000',
- 1
- )
- INSERT INTO [dbo].SCHED_Master
- (
- [ID], [ProjectName], [ProjectType],
- [ProjectTypeName], [SCHED_ST_DT],
- [SCHED_ED_DT], [ACT_ST_DT], [ACT_ED_DT],
- [status]
- )
- VALUES
- (
- 1003, 'Project-1', 3, 'Normal', '2015-06-28 00:00:00.000',
- '2015-09-03 00:00:00.000', '2015-07-02 00:00:00.000',
- '2015-08-24 00:00:00.000', 1
- )
- INSERT INTO [dbo].SCHED_Master
- (
- [ID], [ProjectName], [ProjectType],
- [ProjectTypeName], [SCHED_ST_DT],
- [SCHED_ED_DT], [ACT_ST_DT], [ACT_ED_DT],
- [status]
- )
- VALUES
- (
- 1004, 'Project-2', 1, 'Urgent', '2015-06-28 00:00:00.000',
- '2015-08-02 00:00:00.000', '2015-07-02 00:00:00.000',
- '2015-08-26 00:00:00.000', 1
- )
- INSERT INTO [dbo].SCHED_Master
- (
- [ID], [ProjectName], [ProjectType],
- [ProjectTypeName], [SCHED_ST_DT],
- [SCHED_ED_DT], [ACT_ST_DT], [ACT_ED_DT],
- [status]
- )
- VALUES
- (
- 1005, 'Project-2', 2, 'Important',
- '2015-07-09 00:00:00.000', '2015-12-22 00:00:00.000',
- '2015-06-28 00:00:00.000', '2015-12-14 00:00:00.000',
- 1
- )
- INSERT INTO [dbo].SCHED_Master
- (
- [ID], [ProjectName], [ProjectType],
- [ProjectTypeName], [SCHED_ST_DT],
- [SCHED_ED_DT], [ACT_ST_DT], [ACT_ED_DT],
- [status]
- )
- VALUES
- (
- 1006, 'Project-2', 3, 'Normal', '2015-06-01 00:00:00.000',
- '2015-08-04 00:00:00.000', '2015-06-22 00:00:00.000',
- '2015-08-24 00:00:00.000', 1
- )
select ID,ProjectName,ProjectType,ProjectTypeName,SCHED_ST_DT,SCHED_ED_DT,ACT_ST_DT,ACT_ED_DT,status from SCHED_Master
After creating our table we will create a stored procedure to display the project schedule result using Pivot query.
I will explain each step of my procedure so that you can understand it clearly to make your own with your table formats.
Step 1: Create the procedure with parameter and declare the variable inside procedure to be used in the SP.
Note here I have set the FromDate and ToDate static. You can change this as a parameter from SP to get dynamic results as per your date range.
- Alter PROCEDURE [dbo].[usp_ProjectSchedule_Select] @projectId VARCHAR(10) = '' AS BEGIN -- 1. Declared for setting the Schedule Start and End date
- --1.Start /////////////
- Declare @FromDate VARCHAR(20) = '2015-06-08' --DATEADD(mm,-12,getdate())
- Declare @ToDate VARCHAR(20) = '2016-05-06' --DATEADD(mm, 1, getdate())
- -- used for the pivot table result
- DECLARE @MyColumns AS NVARCHAR(MAX),
- @SQLquery AS NVARCHAR(MAX)
This Temp table is created for geting all the days between the start date and end date to display as the Column Header
- Start IF OBJECT_ID('tempdb..#TEMP_EveryWk_Sndays') IS NOT NULL
- DROP
- TABLE #TEMP_EveryWk_Sndays
- DECLARE @TOTALCount INT
- Select
- @TOTALCount = DATEDIFF(dd, @FromDate, @ToDate);
- WITH d AS
- (
- SELECT
- top (@TOTALCount) AllDays = DATEADD(
- DAY,
- ROW_NUMBER() OVER
- (
- ORDER BY
- object_id
- ),
- REPLACE(@FromDate, '-', '')
- )
- FROM
- sys.all_objects
- )
- SELECT
- distinct DATEADD
- (
- DAY,
- 1 - DATEPART(WEEKDAY, AllDays),
- CAST(AllDays AS DATE)
- ) WkStartSundays,
- 1 as status into #TEMP_EveryWk_Sndays
- FROM
- d
- where
- AllDays <= @ToDate
- AND AllDays >= @FromDate -- test the sample temptable with select query
- -- select * from #TEMP_EveryWk_Sndays
- --///////////// End of 2.

Figure 2: DataGridView
Note: For actual data in pivot list I will display the result as:
“-1” – For End Date of both Schedule and Actual result. In my program I will check for the resultant value, if its “-1” then I will display the text as “END” with red back color which is to notify the user for End date of each project.
“0” – If the result value is “0”, then it means the days are not in any Schedule or Actual days so it should be leaved as blank.
“1” – If the result is “1” is to intimate as the Schedule Start and End days. I will be using Blue color to display the Schedule Days.
“2” - If the result is “1” is to intimate as the Actual Start and End days. I will be using Green color to display the Schedule Days.
This is only a sample procedure that explains a sample program for Project schedule, you can customize this table, procedure and program as per your requirement and set your own rule and status to display the result.
This temp table is created to schedule details with the result, here I have used the Union.
The first query return the Schedule Project result and the 2nd query returns the Actual Project result. Both these queries will be inserted to a Temp Table.
- --3.Start /////////////
- IF OBJECT_ID('tempdb..#TEMP_results') IS NOT NULL
- DROP
- TABLE #TEMP_results
- SELECT
- ProjectName,
- viewtype,
- ProjectType,
- resultnew,
- YMWK INTO #TEMP_results
- FROM
- (
- SELECT
- A.ProjectName ProjectName -- Our Project Name
- ,
- '1-Scd' viewtype -- Our View type first we display Schedule Data and then Actual
- ,
- A.ProjectType ProjectType -- Our Project type here you can use your own status as Urgent,normal and etc
- ,
- Case when cast(
- DATEPART(
- wk,
- max(A.SCHED_ED_DT)
- ) as varchar(2)
- ) = cast(
- DATEPART(wk, WkStartSundays) as varchar(2)
- ) then -1 else case when min(A.SCHED_ST_DT)<= F.WkStartSundays
- AND max(A.SCHED_ED_DT) >= F.WkStartSundays then 1 else 0 end end resultnew -- perfectResult as i expect
- ,
- RIGHT(
- YEAR(WkStartSundays),
- 2
- )+ '-' + 'W' + convert(
- varchar(2),
- Case when len(
- DATEPART(wk, WkStartSundays)
- )= '1' then '0' + cast(
- DATEPART(wk, WkStartSundays) as varchar(2)
- ) else cast(
- DATEPART(wk, WkStartSundays) as varchar(2)
- ) END
- ) as 'YMWK' -- Here we display Year/month and Week of our Schedule which will be displayed as the Column
- FROM
- -- here you can youe your own table
- SCHED_Master A (NOLOCK)
- LEFT OUTER JOIN #TEMP_EveryWk_Sndays F (NOLOCK) ON A.status= F.status
- WHERE
- -- Here you can check your own where conditions
- A.ProjectName like '%' + @projectId
- AND A.status = 1
- AND A.ProjectType in (1, 2, 3)
- AND A.SCHED_ST_DT <= @ToDate
- AND A.SCHED_ED_DT >= @FromDate
- GROUP BY
- A.ProjectName,
- A.ProjectType,
- A.SCHED_ED_DT,
- F.WkStartSundays
- UNION
- -- This query is to result the Actual result
- SELECT
- A.ProjectName ProjectName -- Our Project Name
- ,
- '2-Act' viewtype -- Our View type first we display Schedule Data and then Actual
- ,
- A.ProjectType ProjectType -- Our Project type here you can use your own status as Urgent,normal and etc
- ,
- Case when cast(
- DATEPART(
- wk,
- max(A.ACT_ED_DT)
- ) as varchar(2)
- ) = cast(
- DATEPART(wk, WkStartSundays) as varchar(2)
- ) then -1 else case when min(A.ACT_ST_DT)<= F.WkStartSundays
- AND max(A.ACT_ED_DT) >= F.WkStartSundays then 2 else 0 end end resultnew -- perfectResult as i expect
- ,
- RIGHT(
- YEAR(WkStartSundays),
- 2
- )+ '-' + 'W' + convert(
- varchar(2),
- Case when len(
- DATEPART(wk, WkStartSundays)
- )= '1' then '0' + cast(
- DATEPART(wk, WkStartSundays) as varchar(2)
- ) else cast(
- DATEPART(wk, WkStartSundays) as varchar(2)
- ) END
- ) as 'YMWK' -- Here we display Year/month and Week of our Schedule which will be displayed as the Column
- FROM
- -- here you can youe your own table
- SCHED_Master A (NOLOCK)
- LEFT OUTER JOIN #TEMP_EveryWk_Sndays F (NOLOCK) ON A.status= F.status
- WHERE
- -- Here you can check your own where conditions
- A.ProjectName like '%' + @projectId
- AND A.status = 1
- AND A.ProjectType in (1, 2, 3)
- AND A.ACT_ST_DT <= @ToDate
- AND A.ACT_ED_DT >= @FromDate
- GROUP BY
- A.ProjectName,
- A.ProjectType,
- A.SCHED_ED_DT,
- F.WkStartSundays
- ) q --3.End /////////////





Raja TPosted Sep 14, 2015, 7:24 AM
Nice one Sir, Thanks for sharing
Vaikesh K PPosted Sep 14, 2015, 5:34 AM
Good One
Nilesh JadavPosted Sep 11, 2015, 9:24 AM
Great Article Sir
Harshad PansuriyaPosted Sep 11, 2015, 8:44 AM
Nice Article Sir
सौरभ माहेश्वरीPosted Sep 11, 2015, 3:05 AM
nice article sir..
Sujeet SumanPosted Sep 11, 2015, 2:34 AM
Nice Article
King UmarPosted Sep 10, 2015, 9:55 PM
good one sir..
Ankit BansalPosted Sep 10, 2015, 1:03 AM
good one..thanks...
Pankaj Kumar ChoudharyPosted Sep 9, 2015, 9:41 PM
Nice Article Sir.........
Santhakumar MunuswamyPosted Sep 9, 2015, 3:20 PM
Good work. Thanks for sharing us
Mohammed IbrahimPosted Sep 9, 2015, 2:03 PM
nice share
Shridhar SharmaPosted Sep 9, 2015, 12:46 PM
nice article.thanks for sharing
Debendra DashPosted Sep 9, 2015, 11:16 AM
great sir...
Karthikeyan KPosted Sep 9, 2015, 8:59 AM
Awesome one sir...Thanks for sharing us
RakeshPosted Sep 9, 2015, 8:05 AM
Very Nice Article sir
Rajeesh MenothPosted Sep 9, 2015, 7:22 AM
Good Article Syed Shanu sir..