@@ -14,6 +14,11 @@ You can export the grid to CSV with a click of a button. The current filter, sor
1414
1515When you click the Export button, your browser will receive the resulting file.
1616
17+ #### In this article
18+
19+ * [ Basics] ( #basics )
20+ * [ Programmatic Export] ( #programmatic-export )
21+ * [ Notes] ( #notes )
1722
1823## Basics
1924
@@ -87,6 +92,90 @@ Optionally, you can also set the `GridCsvExport` tag settings under the `GridExp
8792}
8893````
8994
95+ ## Programmatic Export
96+
97+ You can programmatically invoke the export feature of the Grid, by using the following methods exposed on the ` @ref ` of the Grid:
98+
99+ * ` SaveAsExcelFileAsync ` - ` ValueTask ` - sends the exported excel file to the browser for download
100+ * ` ExportToExcel ` - ` MemoryStream ` - returns the exported data as a memory stream
101+
102+ > note The same methods are exposed for exporting an [ Excel file] ({%slug grid-export-excel%}#programmatic-export-from-code).
103+
104+ > caption Invoke the export function from code
105+
106+ ```` CSHTML
107+ @* Send the exported file for download and get the exported data as a memory stream *@
108+
109+ @using System.IO
110+
111+ <TelerikButton OnClick="@(async () => await GridRef.SaveAsCsvFileAsync())">Download the CSV file</TelerikButton>
112+ <TelerikButton OnClick="@GetTheDataAsAStream">Get the Exported Data as a MemoryStream</TelerikButton>
113+
114+ <TelerikGrid Data="@GridData"
115+ @ref="@GridRef"
116+ Pageable="true"
117+ Sortable="true"
118+ Reorderable="true"
119+ FilterMode="@GridFilterMode.FilterRow"
120+ Groupable="true">
121+
122+ <GridToolBar>
123+ <GridCommandButton Command="CsvExport" Icon="file-csv">Export to CSV</GridCommandButton>
124+ <label class="k-checkbox-label"><TelerikCheckBox @bind-Value="@ExportAllPages" />Export All Pages</label>
125+ </GridToolBar>
126+
127+ <GridExport>
128+ <GridCsvExport FileName="telerik-grid-export" AllPages="@ExportAllPages" />
129+ </GridExport>
130+
131+ <GridColumns>
132+ <GridColumn Field="@nameof(SampleData.ProductId)" Title="ID" />
133+ <GridColumn Field="@nameof(SampleData.ProductName)" Title="Product Name" />
134+ <GridColumn Field="@nameof(SampleData.UnitsInStock)" Title="In stock" />
135+ <GridColumn Field="@nameof(SampleData.Price)" Title="Unit Price" />
136+ <GridColumn Field="@nameof(SampleData.Discontinued)" Title="Discontinued" />
137+ <GridColumn Field="@nameof(SampleData.FirstReleaseDate)" Title="Release Date" />
138+ </GridColumns>
139+ </TelerikGrid>
140+
141+ @code {
142+ private TelerikGrid<SampleData> GridRef { get; set; }
143+
144+ private MemoryStream exportedCSVStream { get; set; }
145+
146+ private void GetTheDataAsAStream()
147+ {
148+ exportedCSVStream = GridRef.ExportToCsv();
149+ }
150+
151+ List<SampleData> GridData { get; set; }
152+ bool ExportAllPages { get; set; }
153+
154+ protected override void OnInitialized()
155+ {
156+ GridData = Enumerable.Range(1, 100).Select(x => new SampleData
157+ {
158+ ProductId = x,
159+ ProductName = $"Product {x}",
160+ UnitsInStock = x * 2,
161+ Price = 3.14159m * x,
162+ Discontinued = x % 4 == 0,
163+ FirstReleaseDate = DateTime.Now.AddDays(-x)
164+ }).ToList();
165+ }
166+
167+ public class SampleData
168+ {
169+ public int ProductId { get; set; }
170+ public string ProductName { get; set; }
171+ public int UnitsInStock { get; set; }
172+ public decimal Price { get; set; }
173+ public bool Discontinued { get; set; }
174+ public DateTime FirstReleaseDate { get; set; }
175+ }
176+ }
177+ ````
178+
90179## Notes
91180
92181The CSV export has the following specifics:
0 commit comments