Guide · 2026-11-20

Building a Gantt Chart in ASP.NET C#: Step-by-Step Guide

Building a Gantt chart in ASP.NET C# can turn into a tangle of date calculations, layout code, and updates that become harder as your project grows. A bar that looks right for one month can shift out of place when tasks cross the chart’s date range. And if you add progress, owners, or dependencies, a quick prototype may need a bigger rebuild.

But here's the truth: you can start with a small, maintainable chart using ASP.NET Core MVC, a C# view model, and a Razor view. I’ll walk you through a working baseline, explain how its timeline calculations fit together, and show where to add practical improvements. You’ll finish with a chart you can adapt to your own project plan.

How to create a Gantt chart in ASP.NET C#

This example uses ASP.NET Core MVC and Razor to draw a simple, month-long Gantt chart. C# supplies the tasks and timeline; the view positions each task bar using its start and end dates.

We’ll treat each task’s end date as exclusive. For example, a task starting June 2 and ending June 6 covers June 2 through June 5. That convention keeps the duration calculation straightforward.

  1. Create task and chart models. A task needs a label, a start date, an end date, and a progress value. The chart model holds the displayed date range and marker dates.

public sealed class GanttTask
{
    public int Id { get; init; }
    public string Name { get; init; } = "";
    public DateTime Start { get; init; }
    public DateTime End { get; init; }
    public int Progress { get; init; }
    public string Owner { get; init; } = "";
}

public sealed class GanttViewModel
{
    public DateTime TimelineStart { get; init; }
    public DateTime TimelineEnd { get; init; }
    public IReadOnlyList<DateTime> Markers { get; init; }
        = Array.Empty<DateTime>();
    public IReadOnlyList<GanttTask> Tasks { get; init; }
        = Array.Empty<GanttTask>();
}
  1. Provide sample tasks through a controller. Set the timeline end to the first day outside the displayed range. Create markers every seven days to label the chart without crowding it.

public sealed class GanttController : Controller
{
    public IActionResult Index()
    {
        var timelineStart = new DateTime(2025, 6, 1);
        var timelineEnd = new DateTime(2025, 7, 1);

        var markers = new List<DateTime>();
        for (var day = timelineStart; day < timelineEnd; day = day.AddDays(7))
        {
            markers.Add(day);
        }

        var tasks = new List<GanttTask>
        {
            new()
            {
                Id = 1,
                Name = "Plan requirements",
                Start = new DateTime(2025, 6, 2),
                End = new DateTime(2025, 6, 7),
                Progress = 100,
                Owner = "Maya"
            },
            new()
            {
                Id = 2,
                Name = "Build interface",
                Start = new DateTime(2025, 6, 6),
                End = new DateTime(2025, 6, 17),
                Progress = 60,
                Owner = "Noah"
            },
            new()
            {
                Id = 3,
                Name = "Run acceptance tests",
                Start = new DateTime(2025, 6, 18),
                End = new DateTime(2025, 6, 25),
                Progress = 0,
                Owner = "Ava"
            }
        };

        var model = new GanttViewModel
        {
            TimelineStart = timelineStart,
            TimelineEnd = timelineEnd,
            Markers = markers,
            Tasks = tasks
        };

        return View(model);
    }
}
  1. Render task names and timeline markers in Razor. Add this view at Views/Gantt/Index.cshtml. It calculates each bar’s horizontal position as a percentage of the visible timeline.

@model GanttViewModel
@using System.Globalization

@{
    var spanDays = (Model.TimelineEnd - Model.TimelineStart).TotalDays;
}

<div class="gantt">
    <div class="gantt-heading">
        <div class="gantt-name">Work item</div>
        <div class="gantt-track gantt-scale">
            @foreach (var marker in Model.Markers)
            {
                var markerOffset =
                    (marker - Model.TimelineStart).TotalDays / spanDays * 100;
                <span style="left:@(markerOffset.ToString("0.##",
                    CultureInfo.InvariantCulture))%">
                    @marker.ToString("MMM d")
                </span>
            }
        </div>
    </div>

    @foreach (var task in Model.Tasks)
    {
        var clippedStart = task.Start < Model.TimelineStart
            ? Model.TimelineStart
            : task.Start;
        var clippedEnd = task.End > Model.TimelineEnd
            ? Model.TimelineEnd
            : task.End;
        var left = (clippedStart - Model.TimelineStart).TotalDays
            / spanDays * 100;
        var width = (clippedEnd - clippedStart).TotalDays
            / spanDays * 100;

        <div class="gantt-row">
            <div class="gantt-name">
                <strong>@task.Name</strong>
                <span>@task.Owner · @task.Progress%</span>
            </div>
            <div class="gantt-track">
                <div class="gantt-bar"
                     role="img"
                     aria-label="@task.Name, @task.Progress% complete"
                     style="left:@(left.ToString("0.##",
                         CultureInfo.InvariantCulture))%;
                            width:@(width.ToString("0.##",
                         CultureInfo.InvariantCulture))%">
                    <span class="gantt-progress"
                          style="width:@(task.Progress)%;"></span>
                </div>
            </div>
        </div>
    }
</div>
  1. Style the chart and task bars. This CSS uses a fixed 30-day grid to match the sample timeline. If you change the displayed range, adjust the grid sizing to match its number of days.

.gantt {
    overflow-x: auto;
    font-family: Arial, sans-serif;
    min-width: 760px;
}

.gantt-heading,
.gantt-row {
    display: grid;
    grid-template-columns: 230px minmax(500px, 1fr);
}

.gantt-heading {
    font-weight: 700;
    border-bottom: 1px solid #cbd5e1;
}

.gantt-row {
    min-height: 54px;
    border-bottom: 1px solid #e2e8f0;
}

.gantt-name {
    padding: 10px;
}

.gantt-name span {
    display: block;
    color: #64748b;
    font-size: 12px;
    margin-top: 4px;
}

.gantt-track {
    position: relative;
    min-height: 54px;
    background-image: linear-gradient(to right, #e2e8f0 1px, transparent 1px);
    background-size: calc(100% / 30) 100%;
}

.gantt-scale {
    min-height: 38px;
}

.gantt-scale span {
    position: absolute;
    top: 10px;
    transform: translateX(-50%);
    white-space: nowrap;
    font-size: 12px;
}

.gantt-bar {
    position: absolute;
    top: 15px;
    height: 24px;
    overflow: hidden;
    border-radius: 5px;
    background: #cbd5e1;
}

.gantt-progress {
    display: block;
    height: 100%;
    background: #2563eb;
}
  1. Run the page and check the date math. Confirm that a task starting June 6 appears after the June 2 task, and that its bar ends before June 18. Change a task’s progress value and verify that the filled portion changes while the full bar stays in place.

Here’s why: the chart separates schedule dates from presentation. You can replace the sample tasks with records loaded by your application, while keeping the same view model and rendering approach.

How the timeline calculations work

Each task bar needs two values: its left offset and its width. The offset is the number of days between the timeline start and the task start, divided by the total timeline length. The width uses the task duration in the same way.

For example, on a 30-day chart, a task that starts six days after the timeline begins has a left position of 20%. A five-day duration uses about 16.7% of the chart width.

The Razor view clips task dates to the visible range. This prevents a task beginning before June 1 or ending after June 30 from drawing outside the chart. If your application allows tasks entirely outside the displayed range, filter those out before rendering.

Be consistent about whether an end date is inclusive or exclusive. A five-day task can otherwise appear six days long, a small mismatch that becomes noticeable across a busy schedule.

Adding real project tasks and validation

The controller’s hard-coded examples are useful for testing, but real projects need to load tasks through your application’s normal services. Map the results into GanttTask objects and pass them to the view model.

Validate each task before it reaches the chart. Check that the end date follows the start date, progress stays between 0 and 100, and the task name is present. For instance, reject a task with a June 12 start and a June 10 end.

You might be wondering: what about weekends and holidays? The sample treats every calendar day as a working day. If your project schedules workdays only, calculate working-day offsets and durations on the server, then use those values consistently in the chart.

Keep the displayed range configurable as well. A monthly view suits a short project; a quarterly view can make a longer plan easier to scan. The bars still use the same percentage calculation.

Showing dependencies and improving usability

A basic Gantt chart shows timing, but dependencies explain why one task must wait for another. Add a predecessor ID to your task model, such as a testing task that depends on interface work finishing first.

You can then draw connector lines between bars with SVG or a dedicated client-side layer. For a first version, a clear dependency label beside the task may be easier to maintain than connector geometry.

Color alone should not communicate progress or status. Keep the task name, owner, and percentage visible, and provide an accessible label for each bar. A screen reader can then announce the task and its completion level.

The best part? You can keep the chart useful without turning every visual detail into a custom interaction. Start with readable dates and stable bars, then add hover details or editing only when your workflow needs them.

Testing and extending the chart

Test edge cases before adding more features. Try a task that starts on the first day, one that ends on the final visible day, and one that spans the whole month. Those examples catch many off-by-one errors.

Also test a zero-progress task and a completed task. The background bar should remain visible at zero, while a task at 100% should have its progress fill cover the entire bar.

If the task list grows, add pagination or a date-range filter rather than squeezing every row into one screen. For a 90-day view, update the timeline markers and grid so the chart remains legible on smaller screens.

Let me explain: the right next step depends on how people use the chart. A read-only planning view needs reliable rendering; a scheduling interface also needs rules for editing dates, saving changes, and resolving conflicts.

A project-management option: ONES.com

Value proposition: If your priority is organizing project work rather than building and maintaining a custom chart interface, ONES.com offers project management and knowledge management in one platform. Its modules are sold separately, so you can choose the area that fits your team.

ONES.com product screenshot

ONES.com is powered by ONES Assistant. ONES Project is its project management product and a Jira alternative; ONES Wiki is its knowledge management product and a Confluence alternative. It may suit teams that want a managed project workflow alongside their ASP.NET application.

Application scenarios: A software team building an ASP.NET service could use ONES Project to coordinate sprints, report progress, and track delivery work alongside its custom chart. A distributed engineering group could pair project work in ONES Project with ONES Wiki for maintaining team guidance.

ONES.com offers four deployment options: Cloud, On-Premise, Private Cloud, and Air-gapped. The free plan supports up to 30 seats. Those options can matter when a team has deployment requirements that a custom chart alone does not address.

Consider ONES.com when you need project coordination capabilities around the chart, not as a drop-in Gantt component for ASP.NET. If you need a specific chart embedded in your application, keep building the MVC view or evaluate a charting library separately.

Common challenges and practical fixes

Bars appear one day too long

Problem: The chart counts both the start and end dates, even though the duration calculation expects an exclusive end date. Solution: Choose one convention and apply it in the model, date calculations, and labels. With exclusive ends, June 2 to June 6 represents four calendar days.

Tasks disappear at the edge of the chart

Problem: A task starts before the visible range or ends after it. Solution: Clip its visible start and end to the timeline boundaries, as the Razor example does. Filter out tasks that do not overlap the displayed period.

Progress styling becomes hard to read

Problem: A progress fill blends into the task bar or the surrounding grid. Solution: Use distinct colors with enough contrast, while keeping the completion percentage in text so color is not the only indicator.

The chart becomes cramped on smaller screens

Problem: Task names and timeline columns compete for limited width. Solution: Keep the task label column readable and allow horizontal scrolling for the timeline. Offer a shorter date range when people need a quick overview.

FAQs

Can I build a Gantt chart with ASP.NET Core MVC and Razor?

Yes. You can calculate task positions in a Razor view and style them with CSS, as this example does. C# supplies the task schedule and visible date range; the view turns each task into a positioned bar. This approach works well for a straightforward chart. If you need drag-and-drop scheduling, complex dependencies, or frequent client-side updates, consider adding a JavaScript charting component.

Should I use a charting library or create the chart myself?

Choose a custom Razor chart when your needs are simple and you want control over its appearance. It can show dates, task bars, and progress without adding a separate chart dependency. A charting library may be a better fit when you need interactive editing, dependency connectors, zoom controls, or accessibility features out of the box. Compare those needs with the time you can spend maintaining custom behavior.

How do I show task dependencies?

Add a predecessor reference to your task model so one task can identify the work that must finish first. You can show that relationship as text or a small icon initially. To draw connector lines, render an SVG layer over the chart and calculate line endpoints from each task bar’s position. Also validate the dependency rules so your application can detect cycles or invalid task references.

How can I show only working days in the chart?

The example uses calendar days, so weekends occupy space in the timeline. For a working-day chart, create a consistent calendar that excludes weekends and applicable holidays. Calculate task offsets and durations using that calendar on the server, then use those working-day positions in the view. Keep date labels clear, since the space between two adjacent workdays may represent more than one calendar day.

Conclusion

A practical ASP.NET C# Gantt chart starts with a consistent task model, a clear date range, and reliable calculations for each bar’s position and width. The MVC and Razor example gives you a baseline for showing task dates, ownership, and progress.

Test the boundaries and date conventions before layering on dependencies or editing. If the goal shifts from building a custom chart to coordinating broader project work, ONES.com is another option to consider. Start with the workflow you need, then extend the chart only where it helps people make better project decisions.