【问题标题】:How to identify the datasource to add rows to the datsource binding to datagrid Silverlight?如何识别数据源以将行添加到 datsource 绑定到 datagrid Silverlight?
【发布时间】:2011-02-22 08:40:14
【问题描述】:

我正在用书学习,卡在这里

如果您有对绑定集合的引用(或者可以将 DataGrid 的 ItemsSource 属性的值转换为该类型),那么您应该能够简单地调用其 Add 或 Insert 方法。

我的 xaml 页面

public About()
        {
            InitializeComponent();

            this.Title = ApplicationStrings.AboutPageTitle;


            EntityQuery<Web.Models.Class1> qry = context.GetProductSummaryListQuery();
            //page 167
            qry = qry.OrderBy(p => p.Name);
            //the following is asynchronous, therefore any immediate WORKING ON RESULT
            //must be done in the oparation.completed event
            LoadOperation<Web.Models.Class1> operation = context.Load(qry);
            dataGrid1.ItemsSource = operation.Entities;



            //context.Class1s.
           // bool changes_there = context.HasChanges;
        }

我的班级

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;


namespace Tutorial1.Web.Models
{
    public partial class Class1
    {

        [Key]
        [Editable(false)]
        public int ID { get; set; }
        public string Name { get; set; }
        public string Number { get; set; }
        public decimal ListPrice { get; set; }
        public byte[] ThumbNailPhoto { get; set; }
        public int? QuantityAvailable { get; set; }
        public string Category { get; set; }
        public string SubCategory { get; set; }
        public string Model { get; set; }
        public bool MakeFlag { get; set; }
    }

}

我的服务

namespace Tutorial1.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using Tutorial1.Web.Models;

    // TODO: Create methods containing your application logic.
    [EnableClientAccess()]
    public class ProductPMService : DomainService
    {
        private AdventureWorksLTEntities context = new AdventureWorksLTEntities();

        public IQueryable<Web.Models.Class1> GetProductSummaryList()
        {
            return from p in this.context.Products
                   select new Web.Models.Class1()
                   {
                       ID = p.ProductID,
                       ListPrice = p.ListPrice,
                       Name=p.Name
                   };

        }



        public IQueryable<ProductPM> GetProductsFromDB()
        {
            return from p in context.Products
                   select new ProductPM()
                   {
                       ProductID = p.ProductID,
                       Name = p.Name,
                       ProductNumber = p.ProductNumber,
                       ListPrice = p.ListPrice,
                       ModifiedDate = p.ModifiedDate
                   };

        }//get products

        public void InsertProductToDB(ProductPM the_class)
        {
            Product product = new Product();
            product.Name = the_class.Name;
            product.ProductNumber = the_class.ProductNumber;
            product.ListPrice = the_class.ListPrice;
            product.ModifiedDate = DateTime.Now;


            //concurrency
            //ProductPM originalproduct = ChangeSet.GetOriginal<ProductPM>(the_class);


            context.Products.AddObject(product);
            context.SaveChanges();

            ChangeSet.Associate(the_class, product, UpdateProductPMKeys);
        }//InsertProduct

        private void UpdateProductPMKeys(ProductPM the_class, Product product)
        {//reflecting the generated id back.
            the_class.ProductID = product.ProductID;
            the_class.ModifiedDate = product.ModifiedDate;
        }//reflecting the generated id back ends


        protected override void OnError(DomainServiceErrorInfo errorInfo)
        {
            // Log exception here
                    }

    }
}

我不明白什么是绑定源,以及在单击按钮时我应该在哪里添加一行。

【问题讨论】:

    标签: silverlight datagrid


    【解决方案1】:

    我在您的代码中发现了两个问题。

    1) 在您的 xaml 页面后面的代码中,您将加载操作的结果分配给数据网格的 ItemsSource。

    dataGrid1.ItemsSource = operation.Entities;
    

    问题在于operation.Entities 属于IEnumerable&lt;TEntity&gt; 类型,因此没有添加方法。我建议定义一个 ObservableCollection 并将其用作 ItemsSource 并在加载操作的回调方法中用 operation.Entities 的内容填充它。 也许你可以使用这样的东西(简化版,未经测试):

    public ObservableCollection<Web.Models.Class1> Class1Collection { get; set; }
    
    context.Load<T>(qry, r =>
    {
      if (r.HasError)
      {
        // error handling
      }
      else if (r.Entities.Count() > 0)
      {
        this.Class1Collection.Clear();
        foreach (Web.Models.Class1 c in r.Entities)
        {
          this.Class1Collection.Add(c);
        }
      }
      else 
      {
        // handle case when no Entities were returned
      }
    }, null);
    

    在你的构造函数中使用这个:

    dataGrid1.ItemsSource = this.Class1Collection;
    

    如果您这样做,您可以使用以下方法添加新项目:

    Web.Models.Class1 newItem = new Web.Models.Class1();
    this.Class1Collection.Add(newItem);
    context.GetEntitySet<Web.ModelsClass1>().Add(newItem);
    

    2) 您没有在 DomainService 中为 Class1 定义插入或更新方法。 我对您对 Class1 和 ProductPM 的使用感到有些困惑。他们应该是一样的吗?

    无论如何,如果您想添加 Class1 的新实例,您需要在您的 DomainService 中有一个 InsertClass1(Class1 newObject) 方法(最好也有一个 Update 方法)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 2014-10-17
      • 2010-10-13
      • 2013-10-31
      • 2010-10-12
      • 2012-04-02
      • 1970-01-01
      相关资源
      最近更新 更多