Share via


DataGridView Gantt style chart using C# Winform

Note: You can download the Source Code  from the link ** Source Code Download Link **

Introduction

https://code.msdn.microsoft.com/site/view/file/142363/1/1.png

In this article we can see how to create a simple Gantt Style chart inside DataGridView to display the Project Schedule results. In my previous article I have explained the same for  MVC using AngularJS.(https://code.msdn.microsoft.com/Dynamic-scheduling-using-3532836  ).Few members requested me to do the same for Winform using DataGridView.I have make a simple program with same functionality as my Webform example. Kindly refer my both articles to find more detail about project Scheduling.

Building the Sample

As I have explained in the above article all the business logic to display the project management result has been done in my Stored Procedure. I have been used Pivot query in my Stored Procedure to display the final result. We can see detail about how I have written my procedure to display the result in Code part.

 

Description

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.

 https://code.msdn.microsoft.com/site/view/file/142364/1/3.png

1) Create Database and Table

-- =============================================                                
-- Author      : Shanu                                 
-- Create date : 2015-09-08                                  
-- Description : To Create Database,Table and Sample Insert Query                             
-- Latest                                
-- Modifier    : Shanu                                 
-- Modify date : 2015-09-08                          
-- ============================================= 
--Script to create DB,Table and sample Insert data 
USE MASTER 
GO 
  
-- 1) 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] 
  
-- Insert Query  
  

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 Query  
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 steps 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 as static.You can change this as a parameter from SP to get the 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)

**Step 2 : **We have define our Project Start From Date and End Date.Now we need to search the Project schedule result from the given date.The main aim of the Project Schedule chart is do display the data range as Weeks,Month,Year or Day of any one format with continous result with in range.To get the continues result I will get the Days if Sundays from the Start and End date.I will display the result as Week display so here I have used every week Sunday date and store all the dates to temptable to display the result.

-- 2.This Temp table is to created for  get all the days between the start date and end date to display as the Column Header                                                       
 --2.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.

**Step 3 : **I will join above temp table to actual Schedule table to compare the dates and produce the result.First I will check for the Schedule result and using the union I will combine the result to the Actual result and insert the final result to another temp table to generate our pivot result. 

https://code.msdn.microsoft.com/site/view/file/142363/1/1.png

**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 resultent 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 which explains sample program for Project schedule you can change this table ,procedure and Program as per your requirement.You can set your own rule and status to display the result.

-- 3. This temp table is created toScedule details with result here i have used the Union , 
   --the 1st query return the Schedule Project result and the 2nd query returns the Actual Project result both this query 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 /////////////

Step 4 : Here I will display the final result using the pivot query from the final result of temp table result. 

--4.Start ///////////// 
   
 --here first we get all the YMWK which should be display in Columns we use this in our next pivot query 
select @MyColumns = STUFF((SELECT ',' + QUOTENAME(YMWK)  
                    FROM #TEMP_results 
                    GROUP BY  YMWK 
                    ORDER BY  YMWK 
            FOR XML PATH(''), TYPE 
            ).value('.', 'NVARCHAR(MAX)')  
        ,1,1,'') 
 --here we use the above all YMWK  to disoplay its result as column and row display 
set @SQLquery = N'SELECT ProjectName,viewtype,ProjectType,' + @MyColumns + N' from  
             ( 
                 SELECT  
       ProjectName,  
       viewtype, 
       ProjectType, 
       YMWK, 
        resultnew as resultnew  
    FROM #TEMP_results 
            ) x 
            pivot  
            ( 
                 sum(resultnew) 
                for YMWK in (' + @MyColumns + N')  
            ) p  order by ProjectName, ProjectType,viewtype'
  
exec sp_executesql @SQLquery;

 Here is the complete code for the Stored Procedure. 

-- =============================================                                                                       
-- Author      : Shanu                                                                       
-- Create date : 2015-09-07                                                                      
-- Description : To get all prject Schedule details                                                                      
-- Latest                                                                       
-- Modifier    : Shanu                                                                       
-- Modify date : 2015-09-07                                                                       
-- =============================================                                                                       
--  usp_ProjectSchedule_Select 'Project1'                
--  usp_ProjectSchedule_Select ''                                                                 
-- =============================================                                                                  
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)      
  --// End of 1. 
    
  -- 2.This Temp table is to created for  get all the days between the start date and end date to display as the Column Header                                                       
 --2.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. 
     
   -- 3. This temp table is created toScedule details with result here i have used the Union , 
   --the 1st query return the Schedule Project result and the 2nd query returns the Actual Project result both this query 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 ///////////// 
  
 --4.Start ///////////// 
   
 --here first we get all the YMWK which should be display in Columns we use this in our next pivot query 
select @MyColumns = STUFF((SELECT ',' + QUOTENAME(YMWK)  
                    FROM #TEMP_results 
                    GROUP BY  YMWK 
                    ORDER BY  YMWK 
            FOR XML PATH(''), TYPE 
            ).value('.', 'NVARCHAR(MAX)')  
        ,1,1,'') 
 --here we use the above all YMWK  to disoplay its result as column and row display 
set @SQLquery = N'SELECT ProjectName,viewtype,ProjectType,' + @MyColumns + N' from  
             ( 
                 SELECT  
       ProjectName,  
       viewtype, 
       ProjectType, 
       YMWK, 
        resultnew as resultnew  
    FROM #TEMP_results 
            ) x 
            pivot  
            ( 
                 sum(resultnew) 
                for YMWK in (' + @MyColumns + N')  
            ) p  order by ProjectName, ProjectType,viewtype'
  
exec sp_executesql @SQLquery; 
                                     
END

If we run the procedure the final output will be like this. Here we cans see I will display the result of every week using the pivot query.

https://code.msdn.microsoft.com/site/view/file/142365/1/4.png

2) Create our Windows Form Application in Visual Studio 2015

Prerequisites

Visual Studio 2015. You can download it from here. 

After installing our Visual Studio 2015. Click Start -> Programs-> select Visual Studio 2015- Click Visual Studio 2015.

Click New -> Project - > Select Visual C#->Windows >Windows Forms Application-> Select your project location and enter your application Name.

https://code.msdn.microsoft.com/site/view/file/142367/1/5.png

Design your form. In my form I have added a Textbox for searching the details by Project Name and a button to bind the result. Note I have used my DataGridView helper class to create the DataGridView at runtime instead of design time.Kindly refer my article related to create a DatagridView helper class.(DataGridView Helper Class)

**Form Load: **In Form Load Initialize the DataGridView and add the DataGridView to Panel Control using the Helper Class .After DateGridview Initialized bind the data to Grid.

private void  shanuDatagridViewPaint_Load(object sender, EventArgs e) 
        { 
            MasterGrid_Initialize(); 
            bindData(); 
        }

**Initialize Grid:  
**Using my helper class I will create the DataGridView at runtime. Pass all the parameters like Grid Back Color, Height, Width and all properties to create at runtime like below.

public void  MasterGrid_Initialize() 
      { 
             //First generate the grid Layout Design 
                      Helper.ShanuDGVHelper.Layouts(Master_shanuDGV, Color.White, Color.WhiteSmoke, Color.WhiteSmoke, false, Color.WhiteSmoke, true, Color.FromArgb(112, 128, 144), false, false, false, Color.White, 40, 20, "small");  
  
           //Set Height,width and add panel to your selected control 
                      Helper.ShanuDGVHelper.Generategrid(Master_shanuDGV, pnlGrid, 1000, 600, 10, 10); 
   
           Master_shanuDGV.CellFormatting += new  DataGridViewCellFormattingEventHandler(MasterDGVs_CellFormatting); 
               
               }

As we can see after Initializing the DataGridView I have used the CellFormatting DatagridView Event.
**
CellFormatting DataGridView Event: **In Cellformatting DataGridView I will check for each cell result and set the back color of each cell to display our Gantt style chart inside DataGridView.I will check for the project Type and give each Project Actual and Schedule result with Unique color to see the result in more graphical output.

void MasterDGVs_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)  
       { 
            try
               {                             
       ProjectType = ds.Tables[0].Rows[e.RowIndex]["PrpjectType"].ToString(); 
            if (e.ColumnIndex > 2) 
               { 
      Color color1= Color.FromArgb(116, 176, 30);//Green 
  
  Color color2 = Color.FromArgb(0, 76, 153); //Blue 
  
        if (e.Value.ToString() == "0") 
       { 
               e.Value = ""; 
       } 
  
       if(ProjectType=="1") 
       { 
       color1 = Color.FromArgb(116, 176, 30);//Green 
       color2 = Color.FromArgb(0, 76, 153); //Blue 
       } 
       else if  (ProjectType == "2") 
       { 
   color1 = Color.FromArgb(218, 165, 32);//golden rod 
   color2 = Color.FromArgb(255, 215, 0); //GOLD 
       } 
       else if  (ProjectType == "3") 
       { 
  color1 = Color.FromArgb(147, 112, 219);//medium purple 
    color2 = Color.FromArgb(255, 105, 180); //hot pink 
       }  
  
switch (e.Value.ToString()) 
       { 
         case "-1": 
       e.CellStyle.BackColor = Color.FromArgb(255, 69, 0);  // Orange 
  
        e.CellStyle.SelectionBackColor = Color.FromArgb(255, 69, 0); // Orange 
  
       e.CellStyle.ForeColor = Color.White; 
  
       e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; 
               e.Value = "END";  
               break; 
         case "2": 
e.CellStyle.BackColor = color1; 
         e.CellStyle.SelectionBackColor = color1; 
  
               e.Value = ""; 
               break; 
         case "1": 
       e.CellStyle.BackColor = color2; 
        e.CellStyle.SelectionBackColor = color2; 
         e.Value = ""; 
       break; 
       } 
        } 
       } 
catch (Exception ex)  
{ }  
}

**Search Button Click : **In Button click and in Form Load I will call the bindData() to bind the data to the DatagridView. 

private void  btnSearch_Click(object sender, EventArgs e) 
{ 
                      bindData(); 
  }

 https://code.msdn.microsoft.com/site/view/file/142368/1/2.png

 **bindData() Method: **In this method I will pass the Storded procedure Name and parameters to the Business Logic class. From Business logic Class I will pass the parameter and SP name to DAL Class where is will connect to database and get the result and return as DataSet.The final DataSet result from BL will get in Form and bind the result in DatagridView. 

private void  bindData() 
  { 
              try
                { 
                              // Bind data to DGV. 
                              SortedDictionary<string, string> sd = new  SortedDictionary<string, string>() { }; 
              sd.Add("@projectId", txtProjectID.Text.Trim());                           
    ds = new  ShanuProjectScheduleBizClass().SelectList(sd); 
                 Master_shanuDGV.DataSource = null; 
                        if (ds.Tables[0].Rows.Count > 0) 
                              {                                     Master_shanuDGV.DataSource = ds.Tables[0]; 
                              } 
                      } 
                      catch (Exception ex) 
                      { 
                      } 
  }

You can also extend this program to display the output as more graphic way by using the DatagridviewCell painting event.You can draw your own custom format chart types inside the DatagridviewCell Painting event. 

Note :Connection String

You can find "DBConnection.txt" inside bin folder,Change the connection string to your SQL Server DB Setting.

Change Connection String in Code:

https://i1.code.msdn.s-msft.com/datagridview-gantt-style-16180d03/image/file/142372/1/6.png

You can find a "BizBase.cs"  inside Helper/Biz Folder.Change the connection string to your local SQL Server Connection string.

Note: You can download the Source Code  from the link ** Source Code Download Link **