locked
Databinding and Printing RRS feed

  • Question

  • Hi all.

    Well, since my thread was not even viewed, I'm asking again here. Can anyone help me with binding a CLR object to Blend controls? The problem is, when I try to update any data back to the SQL database, it doesn't work. No errors are generated, but the data is not updated. It is retrieved only. Even setting the binding as Two-Way.

    The other question is, how can I generate and print a report on this data in WPF? Does Crystal Report works with WPF?

    Thanks in advance.
    Friday, July 25, 2008 12:33 PM

All replies

  •   Howdy Jean,

    With the Binding scenario, I am unsure of the methods that you are using so I can only tell you how I do it..

    In a logical manager of my UI, I get a reference to the ObjectDataProvider that I used to bind the elements to the form.. from there I manually manage changes to and from my data provider (be it through web services or direct to SQL) on a background worker thread. I know this appears to be defeating the purpose of binding, but it is very flexible.

    Share some of your object code if you like and I will take a look at it this week to try and understand the route you have taken.


    You will need to head to the cider forums to ask about reporting with WPF as it is outside the scope of this group.

    http://forums.msdn.microsoft.com/en-US/wpf/threads/


    HTH

    Cheers

    Expression MVP

    Learn Expression Blend at learnexpressionstudio.com

    my blog : http://x-coders.com/blogs/sneaky/default.aspx

    Tuesday, July 29, 2008 12:20 PM
    Moderator
  • Hi there,

    Well, thanks for replying. I couldn't log on sooner, though.

    I also used the ObjectDataProvider. In fact, I'm trying to expand one of the Expression Blend examples I found on the user guide. The one about CLR databinding. That example tells us to make the class library and how to map to a WPF ObservableCollection, using an XML datasource. Well, I'm not using XML, I'm using a SQL Server database and tables. The problem is that my database tables are linked in a Master-detail relationship. I remember to have read somewhere that I couldn't make an ObservableCollection out of a table that's part of a relationship, but I'm not sure that's true. I wanted to know how can I make that collection to work with Blend.

    I don't know if this is the place to ask this question, so if not, could you point me to the best spot?

    Here's the code of two of my Collections. CustomerOrders is the namespace.

    The first one is a simple Suppliers table. It has only a primary key (SupplierID).

    public class CSuppliersCollection  
        {  
            private ObservableCollection<CSupplier> _Suppliers =  
                new ObservableCollection<CSupplier>();  
            public ObservableCollection<CSupplier> Suppliers  
            {  
                get { return this._Suppliers; }  
            }  
     
            private DelegateCommand getDataCommand;  
            public DelegateCommand GetDataCommand { get { return getDataCommand; } }  
     
            private void GetData()  
            {  
                CustomerOrdersTableAdapters.TBL_FornecedoresTableAdapter FornecedoresTableAdapter =  
                    new CustomerOrdersTableAdapters.TBL_FornecedoresTableAdapter();  
                CustomerOrders.TBL_FornecedoresDataTable FornecedorDT = FornecedoresTableAdapter.GetData();  
                _Suppliers.Clear();  
                  
                foreach (CustomerOrders.TBL_FornecedoresRow row in FornecedorDT)  
                {  
                    _Suppliers.Add(new CSupplier(  
                        row.SupplierID,  
                        row.Empresa,  
                        row.Entrega,  
                        row.Condicoes));  
                }  
            }  
     
            // Constructor  
            public CSuppliersCollection()  
            {  
                getDataCommand = new DelegateCommand(delegate() { GetData(); });  
            }  
        } 

    The second is the Products Collection. It has a Primary key (ProductID) and a Foreign Key (SupplierID):
    public class CProductsCollection  
        {  
            private ObservableCollection<CProduct> _Products =  
                new ObservableCollection<CProduct>();  
            public ObservableCollection<CProduct> Products  
            {  
                get { return this._Products; }  
            }  
     
            private void GetData()  
            {  
                CustomerOrdersTableAdapters.TBL_ProdutosTableAdapter ProdutosTableAdapter =  
                    new CustomerOrdersTableAdapters.TBL_ProdutosTableAdapter();  
                CustomerOrders.TBL_ProdutosDataTable ProdutoDT = ProdutosTableAdapter.GetData();  
                _Products.Clear();  
                foreach (CustomerOrders.TBL_ProdutosRow row in ProdutoDT)  
                {  
                    _Products.Add(new CProduct(  
                        row.ProductID,  
                        row.SupplierID,  
                        row.Quantidade,  
                        row.Produto,  
                        row.ValorUnitario));  
                }  
            }  
        } 

    As it is in Portuguese, I only changed the key columns names to english, so that it is easier for understanding.

    The problem is. When I bind only one or the other table, it works perfectly, I can retrieve data and update, but when the database tables are linked, updating doesn't work and data is not displayed as it should. Detail data is not correctly linked to the master. I hope I'm not making this too confusing. :)
    Sunday, August 3, 2008 6:18 PM
  • I forgot the WPF mapper classes.
    public class CSupplier  
        {  
            #region Private Properties  
            private int _SupplierID;  
            private string _Empresa;  
            private string _Entrega;  
            private string _Condicoes;  
            private ObservableCollection<CProduct> _Produtos;  
            #endregion  
     
            #region Access Methods  
            public int SupplierID  
            {  
                get { return _SupplierID; }  
            }  
            public string Empresa  
            {  
                get { return _Empresa; }  
                set { _Empresa = value; }  
            }  
            public string Entrega  
            {  
                get { return _Entrega; }  
                set { _Entrega = value; }  
            }  
            public string Condicoes  
            {  
                get { return _Condicoes; }  
                set { _Condicoes = value; }  
            }  
            public ObservableCollection<CProduct> Produtos  
            {  
                get { return new ObservableCollection<CProduct>(this._Produtos); } 
            }  
            #endregion  
     
            public void AddProduct(CProduct product)  
            {  
                this._Produtos.Add(product);  
            }  
     
            // Class Constructor  
            public CReseller(int id, string empresa, string entrega, string condicoes)  
            {  
                _SupplierID = id;  
                _Empresa = empresa;  
                _Entrega = entrega;  
                _Condicoes = condicoes;  
                _Produtos = new ObservableCollection<CProduct>();  
            }  
        } 

    and the CProduct class:
    public class CProduct  
        {  
            #region Private Properties  
            private int _ProductID;  
            private int _SupplierID;  
            private int _Quantidade;  
            private string _Produto;  
            private System.Nullable<decimal> _ValorUnitario;  
            #endregion  
     
            #region Access Methods  
            public int ProductID  
            {  
                get { return _ProductID; }  
            }  
            public int CodFornecedor  
            {  
                get { return _CodFornecedor; }  
                set { _CodFornecedor = value; }  
            }  
            public int Quantidade  
            {  
                get { return _Quantidade; }  
                set { _Quantidade = value; }  
            }  
            public string Produto  
            {  
                get { return _Produto; }  
                set { _Produto = value; }  
            }  
            public System.Nullable<decimal> ValorUnitario  
            {  
                get { return _ValorUnitario; }  
                set { _ValorUnitario = value; }  
            }  
            #endregion  
     
            //Class Constructor  
            public CProduct(int id, int idSupp, int quant, string produto, System.Nullable<decimal> valorUnit)  
            {  
                _ProductID = id;  
                _SupplierID = idSupp;  
                _Quantidade = quant;  
                _Produto = produto;  
                _ValorUnitario = valorUnit;  
            }  
        } 

    Thank you very much for the help.
    Sunday, August 3, 2008 6:24 PM