Export to CSV

Id First Name Last Name Status
370PatrickAdamsInactive
493KarenAdamsActive
570NancyAdamsInactive
599EricAdamsInactive
635ChristineAdamsInactive
663BonnieAdamsInactive
778RonaldAdamsInactive
820SeanAdamsInactive
897JudithAdamsActive
857MichaelAlexanderActive
Showing 1 to 10 of 1000 entries

Code

Javascript in View
<script type="text/javascript">
	$(function () {
		$('#exportButton').click(function () {
			location.href = MVCGrid.getExportUrl('ExportGrid');
		});
	});
</script>
        
Inside MVCGridConfig.cs
MVCGridDefinitionTable.Add("ExportGrid", new MVCGridBuilder<Person>(colDefauls)
    .WithAuthorizationType(AuthorizationType.AllowAnonymous)
    .AddColumns(cols =>
    {
        cols.Add().WithColumnName("Id")
            .WithSorting(false)
            .WithHtmlEncoding(false)
            .WithValueExpression((p, c) =>
            {
                return String.Format("<a href='{0}'>{1}</a>",
                    c.UrlHelper.Action("detail", "demo", new { id = p.Id }), p.Id);
            })
            .WithPlainTextValueExpression(p => p.Id.ToString());
        cols.Add("FirstName").WithHeaderText("First Name")
            .WithValueExpression(p => p.FirstName);
        cols.Add("LastName").WithHeaderText("Last Name")
            .WithValueExpression(p => p.LastName);
        cols.Add("Status").WithSortColumnData("Active")
            .WithHeaderText("Status")
            .WithValueExpression(p => p.Active ? "Active" : "Inactive");
    })
    .WithSorting(true, "LastName")
    .WithPaging(true, 10)
    .WithClientSideLoadingMessageFunctionName("showLoading")
    .WithClientSideLoadingCompleteFunctionName("hideLoading")
    .WithRetrieveDataMethod((context) =>
    {
        var options = context.QueryOptions;
        int totalRecords;
        var repo = DependencyResolver.Current.GetService<IPersonRepository>();
        string sortColumn = options.GetSortColumnData<string>();
        var items = repo.GetData(out totalRecords,
            options.GetLimitOffset(), options.GetLimitRowcount(),
            sortColumn, options.SortDirection == SortDirection.Dsc);
        return new QueryResult<Person>()
        {
            Items = items,
            TotalRecords = totalRecords
        };
    })
);
View
@Html.MVCGrid("ExportGrid")