Click here to Skip to main content
14,932,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I try to translate all reports fields before show it to report designer(from report viewer to report designer). using this class

#region #Usings
using System;
using System.Windows.Forms;
using DevExpress.Data;
using DevExpress.DataAccess.ObjectBinding;
using DevExpress.DataAccess.Sql;
using DevExpress.XtraReports.UI;
using MrSales.MrSLanguages;
using MrSales.MrSModels.dataObjects;
// ...
#endregion #Usings

namespace MrSales.MrSModels.TablesTranslations.TablesTranslations.GeneralReportTranslation
{

    #region #SqlDataSource
    public class GeneralReportTranslation : ObjectDataSource, IDisplayNameProvider
    {
        public GeneralReportTranslation()
        {
        }

        XtraReport report = null;
        public GeneralReportTranslation(ObjectDataSource copyFrom,  XtraReport _report)
        {
            this.LoadFromXml(copyFrom.SaveToXml());
            report = _report;
        }

        // ...
        string IDisplayNameProvider.GetDataSourceDisplayName()
        {
            // Substitute the default datasource display name
            // with a custom one.
            return report.DisplayName;
        }

        string IDisplayNameProvider.GetFieldDisplayName(string[] fieldAccessors)
        {
            // Get a field name form the data member's name. 
            string fieldName = fieldAccessors[fieldAccessors.Length - 1];
            return ChangeNames(fieldName);
        }
        public string ChangeNames(string name)
        {
            string result = string.Empty;
            if (name == "people_data")
            {
                result = strings.PEOPLE_DATA;
            }
            else if (name == "Unit")
            {
                result = strings.UNIT;
            }
            else if (name == "Tax")
            {
                result = strings.TAX_NUM;
            }
            else
            {
                try
                {
                    result = strings.ResourceManager.GetString(name, strings.Culture);
                }
                catch
                {
                    return null;
                }
                
            }
            return result;
        }
        // ...
    }
    #endregion #SqlDataSource
}



in the report designer:
private void loadReportDesign( XtraReport report )
{
	report.DataSource = new GeneralReportTranslation((ObjectDataSource)report.DataSource, report);
	reportDesigner1.OpenReport(report);

}



in the report viewer:
XtraReport report = documentViewer1.DocumentSource as XtraReport;
MrSViews.CompanyConfig.ReportDesigner frm = new MrSViews.CompanyConfig.ReportDesigner(report);
frm.TopMost = false;
frm.Show();



but this error
System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List`1[MrSales.MrSModels.dataObjects.CLSPeopleHistory]' to type 'DevExpress.DataAccess.ObjectBinding.ObjectDataSource'.'



appear at
<pre>report.DataSource = new GeneralReportTranslation((ObjectDataSource)report.DataSource, report);




What I have tried:

i tried to ignoring the report viewer and load the report in the report designer with this code and it work:

CustomerMove report_CustomerMove = new CustomerMove();
                report_CustomerMove.DataSource = new GeneralReportTranslation((ObjectDataSource)report_CustomerMove.DataSource, report);
                reportDesigner1.OpenReport(report_CustomerMove);


it work but i can't repair my code to work fine when i pass the report from report viewer to the report designer.
Posted
Updated 28-Mar-21 0:13am
Comments
BillWoodruff 27-Mar-21 19:22pm
   
ask DevExpress

1 solution

If you use the debugger (comes free with Visual Studio), and put a breakpoint at a line of code previous to the one that's throwing the exception, you'll probably be able to see what's wrong. Since we can't do that for you, it's on you.

Since
GeneralReportTranslation
obviously inherits from ObjectDataSource, it looks to me like report.DataSource isn't derived from type ObjectDataSource.

Try qualifying the types with their respective name spaces and see if that clears it up (or indicates where the error lies).
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900