This is a migrated thread and some comments may be shown as answers.

Manual Report Creation - MVVM & WPF

1 Answer 193 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Joshua
Top achievements
Rank 1
Joshua asked on 15 Apr 2011, 02:57 PM
Hello,

I am attempting to manually create a report in the viewmodel to bind to a WPF reportviewer control.

I cannot use the designer as I have to create the table manually. The report will accept a datasource object (in this instance a List<T> where T is a domain object), create the columns, and then populate the rows. I have done extensive research on the forums and created the column collection and even the entire table. I am unable to get it to show anything once the report is bound to the view.

Any help is appreciated.

-Josh

Here is the xaml for the report viewer:

 

 

 

<telerikReporting:ReportViewer Grid.Row="1" x:Name="ReportViewer1" Report="{Binding DataContext.AutoReport}" Width="500" Height="600" >

 

</telerikReporting:ReportViewer>


And here is the ViewModel:

 

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Telerik.Reporting;
using Telerik.Reporting.Drawing;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Telerik.Reporting;
using Telerik.Reporting.Drawing;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Collections;
  
namespace Client.ViewModels
{
   public class TelerikDummyReportViewModel : ReportViewModelBase
    {
        public TelerikDummyReportViewModel(ContentViewModelBase parent) : base(parent)
        {
              
        }
        #region Properties
  
         
  
       private Report _autoreport;
       public Report AutoReport
       {
           get
           {
               return _autoreport;
           }
           set
           {
               // Check if it's really a change 
               if (value == _autoreport)
                   return;
  
               // Change Report 
               _autoreport = value;
  
               // Notify attached View(s) 
               RaisePropertyChanged("AutoReport");
           }
       
       private List<user> _userDataList;
        public List<user> UserDataList 
        
            get { return _userDataList; } 
            set
            {
                _userDataList = value;
                if (_userDataList.Count() > 0)
                {
                    AutoReport = new DynamicReport(_userDataList[0]);
                    AutoReport.Report.DataSource = UserDataList;
  
                }
                RaisePropertyChanged("UserDataList");
                RaisePropertyChanged("AutoReport");
            
        }
          
  
        #endregion
        internal override void ControlLoaded()
        {
            DependencyCheck();
        }
  
        internal override void DependencyCheck()
        {
              
            GetUsers();
        }
        public void GetUsers()
        {
            ShowIsBusyMessage("Loading ...");
            AuthenticationClient.GetLPUsersCompleted += new EventHandler<GetLPUsersCompletedEventArgs>(AuthenticationClient_GetLPUsersCompleted);
            AuthenticationClient.GetLPUsersAsync();
              
        }
  
        void AuthenticationClient_GetLPUsersCompleted(object sender, GetLPUsersCompletedEventArgs e)
        {
            AuthenticationClient.GetLPUsersCompleted -= AuthenticationClient_GetLPUsersCompleted;
            if (e.Error != null)
            {
                this.HandlePrompt("Error retrieving audit activity filter.", DisplayName, e.Error);
                HideIsBusyMessage();
            }
            else if (e.Result != null)
            {
                UserDataList = e.Result.ToList();
            }
            HideIsBusyMessage();
        }
  
  
     
       internal class DynamicReport : Telerik.Reporting.Report
       {
           private Table table1;
           public DynamicReport(object sourceObject)
           {
               table1 = createTable(sourceObject);
  
           }
  
  
           private Table createTable(object sourceObject)
           {
               var targetTable = new Table();
               targetTable.ColumnGroups.Clear();
               targetTable.Body.Columns.Clear();
               targetTable.Body.Rows.Clear();
  
               var sourcePropertyList = sourceObject.GetType().GetProperties();
               Telerik.Reporting.TextBox textboxGroup;
               Telerik.Reporting.TextBox textBoxTable;
               Telerik.Reporting.TableGroup tableGroupColumn;
               int i = 0;
               foreach (var o in sourcePropertyList)
               {
                   tableGroupColumn = new TableGroup();
  
                   targetTable.Body.Columns.Add(new TableBodyColumn(Unit.Inch(1)));
  
                   textboxGroup = new Telerik.Reporting.TextBox();
                   textboxGroup.Value = o.Name;
                   textboxGroup.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
                   tableGroupColumn.ReportItem = textboxGroup;
                   targetTable.ColumnGroups.Add(tableGroupColumn);
  
                   textBoxTable = new Telerik.Reporting.TextBox();
                   targetTable.Body.SetCellContent(0, i++, textBoxTable);
                   textBoxTable.Value = "=Fields." + o.Name;
               }
               TableGroup tableGroupRow = new TableGroup();
               targetTable.RowGroups.Add(tableGroupRow);
               tableGroupRow.Grouping.Add(new Telerik.Reporting.Data.Grouping());
               targetTable.DataSource = sourceObject;
               return targetTable;
           }
       }
          
    }
}

1 Answer, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 20 Apr 2011, 01:31 PM
Hello Joshua,

By the looks of it you're trying to inherit our report viewer and make changes to it. If you need to modify the WPF viewer template, you can go ahead and modify it as explained in the Creating Style in Expression Blend help article. If your goal is different, then please elaborate, so we can provide adequate suggestions. If you're creating your own model, take a look at the ReportProcessor Class.

Regards,
Steve
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
Joshua
Top achievements
Rank 1
Answers by
Steve
Telerik team
Share this question
or