Search code examples
c#winformsdevexpressxtrareport

Report viewer in XtraReports C#.Net using Winforms Devexpress?


Hi I created three reports using XtraReports. I can able to view that in button click using this code

 xtrareport1 report = new xtrareport1 (Convert.ToInt32(TXE_CompanyId.Text));
 ReportPrintTool tool = new ReportPrintTool(report);
 tool.ShowPreview();

I need to show this three reports name in LookupEdit and user select a report means need to show that preview in PrintControl1 ? How to do this ?


Solution

  • If you want to get all reports in your project then you can use the solution provided by DevExpress:

    You can use the Assembly.GetExecutingAssembly().GetTypes() method to get all classes defined in the assembly. Then you can cycle through the list to find classes derived from the XtraReport class. To create a new instance, use the Activator.CreateInstance() method.

    Here is example of implementation:

    public Form1()
    {
        InitializeComponent();
    
        var reports = new List<Tuple<string, Type>>();
    
        foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
            if (type.IsSubclassOf(typeof(XtraReport)))
                reports.Add(new Tuple<string, Type>(type.Name, type));
    
        lookUpEdit1.Properties.DataSource = reports;
        lookUpEdit1.Properties.DisplayMember = "Item1";
        lookUpEdit1.Properties.ValueMember = "Item2";
        lookUpEdit1.Properties.Columns.Add(new LookUpColumnInfo("Item1","Report"));
    }
    
    private void simpleButton1_Click(object sender, EventArgs e)
    {
        var value = lookUpEdit1.EditValue;
    
        if (value == null)
            return;
    
        var report = (XtraReport)Activator.CreateInstance((Type)value);
    
        var tool = new ReportPrintTool(report);
        tool.ShowPreview();
    }