To create Crystal Reports in ASP.NET MVC from a DataGridView, you can follow these steps:
- Install the Crystal Reports for Visual Studio NuGet package in your ASP.NET MVC project.
- Create a new Crystal Report in your project:
- Right-click on the project in the Solution Explorer and select Add > New Item.
- Choose Reporting > Crystal Report and name the report.
- Select Create a new Crystal Report using the Report Wizard and click OK.
- In the Report Wizard, choose the data source for your report (e.g., Database, Object, or XML).
- Design your Crystal Report by adding fields, grouping, sorting, and formatting as needed.
- Create a new controller action to generate the report:
csharp
public ActionResult GenerateReport()
{
// Retrieve data from the DataGridView or any other data source
var data = GetDataFromDataGridView();
// Create a new instance of the Crystal Report
var report = new YourReportName();
report.SetDataSource(data);
// Export the report to a stream
var stream = report.ExportToStream(ExportFormatType.PortableDocFormat);
// Return the report as a file
return new FileContentResult(stream.ToArray(), “application/pdf”)
{
FileDownloadName = “Report.pdf”
};
}
5. Create a view to display a link or button that triggers the report generation:
xml
<a href=”@Url.Action(“GenerateReport”, “YourControllerName”)”>
Generate Report
</a>
When the user clicks the link or button, it will execute the GenerateReport
action method, which will retrieve the data from the DataGridView, create the Crystal Report, export it to a PDF stream, and return the report as a file.Note: Make sure to replace YourReportName
with the actual name of your Crystal Report and YourControllerName
with the name of your controller.
Also, make sure that Crystal Reports is properly configured in your ASP.NET MVC project, including adding references, configuring the report reader, and managing any necessary dependencies.