Ajay Bhaskar
Ajay Bhaskar

Reputation: 63

How to set datasource in report viewer at runtime in devexpress winform application?

I am trying to display report in Devexpress winform. Below is what I have tried so far.

string test = @"C:\Users\bajay\Desktop\First.repx";

if (File.Exists(test))
     MessageBox.Show("Test");
     
XtraReport report = new XtraReport();
report.LoadLayout(test);
var tool = new ReportPrintTool(report);
    tool.ShowPreview();

Upvotes: 0

Views: 5692

Answers (3)

jigar prajapati
jigar prajapati

Reputation: 17

No you're wrong to go, because you just load the .REPX file which have actually not database. It's have only layout, you should have to connect database like SQL, JSONDataSource, WCF or etc.. via SQLDataSource Component, you should have to connect at design time once and do as you want to do code, after at run time just change the connection string, but make sure and remember to add XPO in connection string before.

Understand in better manners. 1. Connect as design time the to database 2. Make punishment of your project 3. Simple change the connection string on client machine 4.And you will go to done!

Upvotes: 1

Niranjan Singh
Niranjan Singh

Reputation: 18290

I suggest you go through Provide Data to a Report of the XtraReport documentation.

There you will find document lists the design-time tutorials and runtime samples that illustrate how to connect reports and their elements (such as calculated fields and parameters) to various kinds of data sources.

Example:

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using DevExpress.XtraReports.UI;
// ... 

private void Form1_Load(object sender, EventArgs e) {
    XtraReport1 report = new XtraReport1();
    report.DataSource = CreateData();

    ReportPrintTool tool = new ReportPrintTool(report);
    tool.ShowPreview();
}

private  List<Data> CreateData() {
    List<Data> data = new List<Data>();

    Data item1 = new Data();
    item1.Date = DateTime.Now;
    item1.Id = 0;
    item1.Name = "First";
    data.Add(item1);

    Data item2 = new Data();
    item2.Date = DateTime.Now;
    item2.Id = 1;
    item2.Name = "Second";
    data.Add(item2);

    Data item3 = new Data();
    item3.Date = DateTime.Now;
    item3.Id = 2;
    item3.Name = "Third";
    data.Add(item3);

    return data;
}

References:
How to: Bind a Report to a Data Source Schema
How to: Bind a Report to a List Object at Design Time and Provide Data at Runtime

How to: Bind a Report to an Array List
How to: Bind a Report to a Collection that Implements the ITypedList Interface
How to: Bind a Report to an XML File (Runtime Sample)

Hope this help..

Upvotes: 0

Sancho Panza
Sancho Panza

Reputation: 759

You have to provide data using the DataSource-property of the report. This could be a DataTable filled from your database or an IEnumrable<>/List of objects.

report.DataSource = YourItemFactory.GetYourItems();

You may refer to the DX-support pages regarding this topic.

Upvotes: 1

Related Questions