Resolving datasource object type on bands

dannidanni Denmark
edited 3:29AM in FastReport .NET
hi

I use the current method to discover the type of datasource on bands:

private List<String> ResolveDataSources(Report report)
        {
              List<String> list = new List<String>();

              foreach (PageBase pb in report.Pages)
              {
                  
                  foreach (var obj in pb.AllObjects)
                  {
                      if (obj is FastReport.DataBand)
                      {
                          if ((obj as FastReport.DataBand).DataSource != null &&
                              (obj as FastReport.DataBand).DataSource.DataType != null)
                              list.Add((obj as FastReport.DataBand).DataSource.DataType.GetGenericArguments()[0].Name);
                      }

                  }
              }
            return list;
        }

Can you tell me why this does not work before datasource is set?

I use the list<string> of types to determin shich data should be loaded for the report.

Thanks.

Comments

  • dannidanni Denmark
    edited November 2011
    A report saved as a blob/text to my database contains a datatype. But when I load it into Fastreport.Report object it is null.

    Can this be correct? (I can see the type after data is registered, but then it is too late)

    I am missing the datatype value in order to minimize the number of calls to the database when I fetch data for the report.
  • edited 3:29AM
    Hello,

    This code works well for me if I insert in in the Features/Business Objects demo report:
        private void ResolveDataSources(Report report)
        {
          foreach (PageBase pb in report.Pages)
          {
            foreach (Base obj in pb.AllObjects)
            {
              if (obj is FastReport.DataBand)
              {
                if ((obj as FastReport.DataBand).DataSource != null &&
                  (obj as FastReport.DataBand).DataSource.DataType != null)
                  MessageBox.Show((obj as FastReport.DataBand).DataSource.DataType.GetGenericArguments()[0].Name);
              }
    
            }
          }
        }
        
        private void _StartReport(object sender, EventArgs e)
        {
          ResolveDataSources(Report);
        }
    

    The _StartReport event handler is connected to the Report.StartReport.
  • dannidanni Denmark
    edited November 2011
    Hey Alex.

    It seems start is fired when datasource is bound the first time? I would like to know the types before retrieving data from my server environment.


    I have modified my example it a bit, so it works as I would like it to do.

    It is a bit of a hack because I register an empty datasource, then resolve types from the bands.

    Now I can reduce the Loading of data with the resolved types from the bands, Meaning I only fetch data needed to preview the report. Awsome! >
    public class ResolveDataSources<T> where T : BaseReportObject
        {
            private readonly Report _report;
    
            public ResolveDataSources(Report report)
            {
                _report = report;
            }
    
            public List<string> Resolve()
            {
                T item = (T)Activator.CreateInstance(typeof (T));
                new RegisterFastReportData<T>(_report, new List<T> {item}).RegisterData(); //registers a dummy datasource of the required type (T)
    
                return GetDataTypesOfBands(_report); //returns the Type used in datasources
            }
    
            private List<string> GetDataTypesOfBands(Report report)
            {
                List<string> list = new List<string>();
    
                foreach (PageBase pb in report.Pages)
                {
                    foreach (DataBand databand in pb.AllObjects.OfType<DataBand>())
                    {                    
                        if (databand.DataSource != null && databand.DataSource.DataType != null)
                            list.Add(databand.DataSource.DataType.GetGenericArguments()[0].Name);
                    }
                }
                return list;
            }
        }
    

Leave a Comment