|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Integrating Blazor DataGrid with GitHub Codespaces | Syncfusion |
| 4 | +description: Step by step guide to integrate Blazor DataGrid in a Blazor Web App using GitHub Codespaces with development container setup and cloud based execution. |
| 5 | +platform: Blazor |
| 6 | +control: Common |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Integrating Blazor DataGrid with GitHub Codespaces |
| 11 | + |
| 12 | +This article explains how to integrate the **[Blazor DataGrid](https://www.syncfusion.com/blazor-components/blazor-datagrid)** and run it seamlessly in **[GitHub Codespaces](https://docs.github.com/en/codespaces/about-codespaces/what-are-codespaces)**. |
| 13 | + |
| 14 | +GitHub Codespaces provides a cloud-based development environment that eliminates the need for local setup and enables instant development in Visual Studio Code directly in the browser. |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +Before getting started, ensure you have the following: |
| 19 | + |
| 20 | +* A [GitHub](https://github.com/) account |
| 21 | +* Access to [GitHub Codespaces](https://docs.github.com/en/codespaces/about-codespaces/what-are-codespaces) |
| 22 | + |
| 23 | +## Configure a development container for .NET 10 and Blazor |
| 24 | + |
| 25 | +To run Blazor applications in GitHub Codespaces, configure a development container with the **.NET 10 SDK** and support for **ASP.NET Core and Blazor development**. |
| 26 | + |
| 27 | +### Prerequisites for dev container setup |
| 28 | + |
| 29 | +* A local [Git](https://git-scm.com/) client installed on your machine |
| 30 | +* Your repository cloned locally or access to create files through the [GitHub web interface](https://docs.github.com/en/repositories/working-with-files/managing-files/creating-new-files) |
| 31 | + |
| 32 | +### Create the dev container configuration |
| 33 | + |
| 34 | +#### Step 1: Clone your repository |
| 35 | + |
| 36 | +Clone your GitHub repository to your local machine. |
| 37 | + |
| 38 | +{% tabs %} |
| 39 | +{% highlight bash tabtitle="Terminal" %} |
| 40 | + |
| 41 | +git clone <your-repo-url> |
| 42 | +cd <your-repo> |
| 43 | + |
| 44 | +{% endhighlight %} |
| 45 | +{% endtabs %} |
| 46 | + |
| 47 | +You can also create files directly in GitHub by navigating to your repository and selecting **Add file → Create new file**. |
| 48 | + |
| 49 | +#### Step 2: Create the `.devcontainer` folder |
| 50 | + |
| 51 | +Create a folder named `.devcontainer` at the root level of your repository. |
| 52 | + |
| 53 | +{% tabs %} |
| 54 | +{% highlight bash tabtitle="Terminal" %} |
| 55 | + |
| 56 | +mkdir .devcontainer |
| 57 | + |
| 58 | +{% endhighlight %} |
| 59 | +{% endtabs %} |
| 60 | + |
| 61 | +#### Step 3: Add the `devcontainer.json` file |
| 62 | + |
| 63 | +Inside the `.devcontainer` folder, create a file named `devcontainer.json` and add the following configuration. GitHub Codespaces automatically applies the settings from this file when a codespace starts. Add it to your repository before launching Codespaces so the environment is configured correctly. |
| 64 | + |
| 65 | +{% tabs %} |
| 66 | +{% highlight json tabtitle=".devcontainer/devcontainer.json" %} |
| 67 | + |
| 68 | +{ |
| 69 | + "name": "Blazor (.NET 10) Codespaces Development Container", |
| 70 | + "image": "mcr.microsoft.com/devcontainers/dotnet:10.0", |
| 71 | + "features": { |
| 72 | + "ghcr.io/devcontainers/features/github-cli:1": {} |
| 73 | + }, |
| 74 | + "customizations": { |
| 75 | + "vscode": { |
| 76 | + "extensions": [ |
| 77 | + "ms-dotnettools.csharp", |
| 78 | + "ms-dotnettools.csdevkit", |
| 79 | + "ms-dotnettools.vscodeintellicode-csharp", |
| 80 | + "ms-dotnettools.blazor-tools", |
| 81 | + "ms-azuretools.vscode-docker", |
| 82 | + "GitHub.codespaces" |
| 83 | + ] |
| 84 | + } |
| 85 | + }, |
| 86 | + "forwardPorts": [5000], |
| 87 | + "portsAttributes": { |
| 88 | + "5000": { |
| 89 | + "label": "Blazor HTTP", |
| 90 | + "onAutoForward": "openBrowser", |
| 91 | + "requireLocalPort": false |
| 92 | + } |
| 93 | + }, |
| 94 | + "postCreateCommand": "dotnet workload install wasm-tools", |
| 95 | + "postStartCommand": "dotnet restore || true", |
| 96 | + "updateContentCommand": "dotnet workload update", |
| 97 | + "remoteUser": "vscode", |
| 98 | + "remoteEnv": { |
| 99 | + "DOTNET_SYSTEM_GLOBALIZATION_INVARIANT": "false", |
| 100 | + "ASPNETCORE_ENVIRONMENT": "Development", |
| 101 | + "ASPNETCORE_URLS": "http://0.0.0.0:5000", |
| 102 | + "DOTNET_CLI_TELEMETRY_OPTOUT": "true" |
| 103 | + }, |
| 104 | + "waitFor": "postCreateCommand" |
| 105 | +} |
| 106 | + |
| 107 | +{% endhighlight %} |
| 108 | +{% endtabs %} |
| 109 | + |
| 110 | +#### Key configuration details |
| 111 | + |
| 112 | +* **Base image**: Uses the official .NET 10 development container image |
| 113 | +* **Features**: Includes [GitHub CLI](https://cli.github.com/) for repository operations within Codespaces |
| 114 | +* **VS Code extensions**: Installs [C# Dev Kit](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit), Blazor tools, and [Docker](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker) support automatically |
| 115 | +* **Port forwarding**: Uses `forwardPorts` to expose the HTTP port (5000), enabling external access to the application running inside the Codespaces container |
| 116 | +* **WebAssembly (WASM) tools**: Installs Blazor WebAssembly development tools via `workload install` |
| 117 | +* **Environment variables**: Configures [.NET globalization](https://learn.microsoft.com/en-us/dotnet/core/extensions/globalization), development environment, and both protocol URLs |
| 118 | +* **Post-create command**: Automatically restores NuGet packages and installs required workloads after container setup |
| 119 | +* **Post-start restoration**: Runs `dotnet restore` on each container start, and the `|| true` ensures the container starts successfully even if restore produces non-critical warnings |
| 120 | + |
| 121 | +This configuration ensures your Codespaces environment is ready to build and run Blazor applications without any manual setup. |
| 122 | + |
| 123 | +#### Step 4: Commit and push to GitHub |
| 124 | + |
| 125 | +Commit the `.devcontainer` folder to your repository. |
| 126 | + |
| 127 | +{% tabs %} |
| 128 | +{% highlight bash tabtitle="Terminal" %} |
| 129 | + |
| 130 | +git add .devcontainer/devcontainer.json |
| 131 | +git commit -m "Add dev container configuration for Blazor development in GitHub Codespaces" |
| 132 | +git push origin main |
| 133 | + |
| 134 | +{% endhighlight %} |
| 135 | +{% endtabs %} |
| 136 | + |
| 137 | +## Launch GitHub Codespaces |
| 138 | + |
| 139 | +After adding the dev container configuration to your repository, launch GitHub Codespaces: |
| 140 | + |
| 141 | +1. Open your GitHub repository in the browser. |
| 142 | +2. Click the **Code** button. |
| 143 | +3. Select the **Codespaces** tab. |
| 144 | +4. Click **Create codespace on main**. |
| 145 | + |
| 146 | +GitHub Codespaces automatically performs the following actions: |
| 147 | + |
| 148 | +* Provisions a cloud-based development environment |
| 149 | +* Detects the `.devcontainer/devcontainer.json` configuration |
| 150 | +* Installs and configures the .NET 10 development container |
| 151 | +* Installs required VS Code extensions for [C#](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp), Blazor tools, [Docker](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-docker), and [GitHub CLI](https://cli.github.com/) |
| 152 | +* Executes the post-create command to restore NuGet packages |
| 153 | +* Installs Blazor WebAssembly workload tools |
| 154 | +* Launches [Visual Studio Code](https://code.visualstudio.com/) in the browser |
| 155 | + |
| 156 | +After Codespaces finishes initializing, verify the setup: |
| 157 | + |
| 158 | +1. Open the terminal in Codespaces. |
| 159 | +2. Run `dotnet --list-sdks` to confirm that the .NET 10 SDK is installed. |
| 160 | +3. Run `dotnet workload list` to verify that the `wasm-tools` workload is present. |
| 161 | +4. Check the terminal output for any errors from the post-create command. |
| 162 | + |
| 163 | +If the setup encounters errors, review the container logs or rebuild the codespace. |
| 164 | + |
| 165 | +## Create a Blazor Web App |
| 166 | + |
| 167 | +In the Codespaces root terminal, run the following commands to create a new **Blazor Web App (Interactive Server)**. |
| 168 | + |
| 169 | +{% tabs %} |
| 170 | +{% highlight bash tabtitle="Terminal" %} |
| 171 | + |
| 172 | +dotnet new blazor -o BlazorApp --interactivity Server |
| 173 | +cd BlazorApp |
| 174 | + |
| 175 | +{% endhighlight %} |
| 176 | +{% endtabs %} |
| 177 | + |
| 178 | +## Install required NuGet packages |
| 179 | + |
| 180 | +Install the [Syncfusion.Blazor.Grid](https://www.nuget.org/packages/Syncfusion.Blazor.Grid/) and [Syncfusion.Blazor.Themes](https://www.nuget.org/packages/Syncfusion.Blazor.Themes/) NuGet packages. All Syncfusion Blazor packages are available on [nuget.org](https://www.nuget.org/packages?q=syncfusion.blazor). See the [NuGet packages](https://blazor.syncfusion.com/documentation/nuget-packages) topic for details. |
| 181 | + |
| 182 | +{% tabs %} |
| 183 | +{% highlight bash tabtitle="Terminal" %} |
| 184 | + |
| 185 | +dotnet add package Syncfusion.Blazor.Grid -v {{ site.releaseversion }} |
| 186 | +dotnet add package Syncfusion.Blazor.Themes -v {{ site.releaseversion }} |
| 187 | + |
| 188 | +{% endhighlight %} |
| 189 | +{% endtabs %} |
| 190 | + |
| 191 | +## Add required namespaces |
| 192 | + |
| 193 | +After the packages are installed, open the `~/_Imports.razor` file in Blazor Web App and import the `Syncfusion.Blazor` and `Syncfusion.Blazor.Grids` namespaces. |
| 194 | + |
| 195 | +{% tabs %} |
| 196 | +{% highlight razor tabtitle="_Imports.razor" %} |
| 197 | + |
| 198 | +@using Syncfusion.Blazor |
| 199 | +@using Syncfusion.Blazor.Grids |
| 200 | + |
| 201 | +{% endhighlight %} |
| 202 | +{% endtabs %} |
| 203 | + |
| 204 | +## Register Blazor service |
| 205 | + |
| 206 | +Open the `~/Program.cs` file in Blazor Web App and register the Blazor service to enable [Blazor components](https://www.syncfusion.com/blazor-components) in the application. |
| 207 | + |
| 208 | +{% tabs %} |
| 209 | +{% highlight C# tabtitle="Program.cs" hl_lines="1 9" %} |
| 210 | + |
| 211 | +using Syncfusion.Blazor; |
| 212 | + |
| 213 | +var builder = WebApplication.CreateBuilder(args); |
| 214 | + |
| 215 | +builder.Services.AddRazorComponents() |
| 216 | + .AddInteractiveServerComponents(); |
| 217 | + |
| 218 | +// Register Blazor service |
| 219 | +builder.Services.AddSyncfusionBlazor(); |
| 220 | + |
| 221 | +var app = builder.Build(); |
| 222 | + |
| 223 | +{% endhighlight %} |
| 224 | +{% endtabs %} |
| 225 | + |
| 226 | +## Add stylesheet and script resources |
| 227 | + |
| 228 | +The theme stylesheet and script can be accessed from NuGet through [Static Web Assets](https://blazor.syncfusion.com/documentation/appearance/themes#static-web-assets). Include the [stylesheet](https://blazor.syncfusion.com/documentation/appearance/themes) and [script references](https://blazor.syncfusion.com/documentation/common/adding-script-references) in the `~/App.razor` file. |
| 229 | + |
| 230 | +{% tabs %} |
| 231 | +{% highlight html tabtitle="App.razor" %} |
| 232 | + |
| 233 | +<head> |
| 234 | + ... |
| 235 | + <link href="_content/Syncfusion.Blazor.Themes/fluent2.css" rel="stylesheet" /> |
| 236 | +</head> |
| 237 | + |
| 238 | +<body> |
| 239 | + ... |
| 240 | + <script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script> |
| 241 | +</body> |
| 242 | + |
| 243 | +{% endhighlight %} |
| 244 | +{% endtabs %} |
| 245 | + |
| 246 | +## Configure render mode (Server) |
| 247 | + |
| 248 | +For Server render mode, if your app's interactivity location is set to `Per page/component`, add the following directive at the top of each `~/Pages/*.razor` file that requires interactive Server components. |
| 249 | + |
| 250 | +**Per-page directive (Server)** |
| 251 | + |
| 252 | +{% tabs %} |
| 253 | +{% highlight razor %} |
| 254 | + |
| 255 | +@rendermode InteractiveServer |
| 256 | + |
| 257 | +{% endhighlight %} |
| 258 | +{% endtabs %} |
| 259 | + |
| 260 | +## Add Blazor DataGrid component |
| 261 | + |
| 262 | +Open a Razor file located in the `~/Pages/*.razor` (for example, `Home.razor`) and add the [Blazor DataGrid](https://www.syncfusion.com/blazor-components/blazor-datagrid) component inside the razor file. |
| 263 | + |
| 264 | +{% tabs %} |
| 265 | +{% highlight razor tabtitle="Home.razor" %} |
| 266 | + |
| 267 | +@page "/" |
| 268 | +@rendermode InteractiveServer |
| 269 | + |
| 270 | +<h3>Blazor DataGrid in Codespaces</h3> |
| 271 | + |
| 272 | +<SfGrid DataSource="@Orders" /> |
| 273 | + |
| 274 | +@code { |
| 275 | + public List<Order> Orders { get; set; } = new List<Order>(); |
| 276 | + |
| 277 | + protected override void OnInitialized() |
| 278 | + { |
| 279 | + var customers = new string[] { "James Hopper", "Michael Smith", "Sarah Johnson", "Robert Davis", "Emily Wilson" }; |
| 280 | + var cities = new string[] { "New York", "Los Angeles", "Chicago", "Houston", "Phoenix" }; |
| 281 | + var rng = new Random(); |
| 282 | + Orders = Enumerable.Range(1, 10).Select(x => new Order() |
| 283 | + { |
| 284 | + OrderID = 1000 + x, |
| 285 | + CustomerName = customers[rng.Next(customers.Length)], |
| 286 | + ShipCity = cities[rng.Next(cities.Length)], |
| 287 | + Freight = Math.Round(10.5 + (x * 7.3), 2), |
| 288 | + OrderDate = DateTime.Now.AddDays(-x), |
| 289 | + }).ToList(); |
| 290 | + } |
| 291 | + |
| 292 | + public class Order { |
| 293 | + public int? OrderID { get; set; } |
| 294 | + public string CustomerName { get; set; } = string.Empty; |
| 295 | + public string ShipCity { get; set; } = string.Empty; |
| 296 | + public DateTime? OrderDate { get; set; } |
| 297 | + public double? Freight { get; set; } |
| 298 | + } |
| 299 | +} |
| 300 | + |
| 301 | +{% endhighlight %} |
| 302 | +{% endtabs %} |
| 303 | + |
| 304 | +## Run the application in Codespaces |
| 305 | + |
| 306 | +In the Codespaces terminal, run: |
| 307 | + |
| 308 | +{% tabs %} |
| 309 | +{% highlight bash tabtitle="Terminal" %} |
| 310 | + |
| 311 | +dotnet run --urls=http://0.0.0.0:5000 |
| 312 | + |
| 313 | +{% endhighlight %} |
| 314 | +{% endtabs %} |
| 315 | + |
| 316 | +### Access the application |
| 317 | + |
| 318 | +After running: |
| 319 | + |
| 320 | +1. Codespaces automatically detects the running ports. |
| 321 | +2. Open the **Ports** panel in the VS Code bottom panel. |
| 322 | +3. Click **Open in Browser** on the HTTP port (5000) for the best experience. |
| 323 | + |
| 324 | +The Blazor application loads with the DataGrid displaying 10 order records. The grid is fully interactive and runs within the Codespaces browser environment. |
| 325 | + |
| 326 | + |
| 327 | + |
| 328 | +## See also |
| 329 | + |
| 330 | +* [Getting Started with Blazor DataGrid in Blazor Web App](https://blazor.syncfusion.com/documentation/datagrid/getting-started-with-web-app) |
| 331 | +* [Integrating Blazor DataGrid with PDF Viewer](https://blazor.syncfusion.com/documentation/common/integration/blazor-with-pdf-viewer) |
| 332 | +* [Integrating Blazor DataGrid with Spreadsheet](https://blazor.syncfusion.com/documentation/common/integration/blazor-grid-with-spreadsheet) |
| 333 | +* [Integrating Blazor DataGrid with Bold Report Viewer](https://blazor.syncfusion.com/documentation/common/integration/blazor-datagrid-boldreports) |
0 commit comments