我正在使用一本书来学习,而且我被困在这里
如果你有对绑定集合的引用(或者可以将DataGrid的ItemsSource属性的值转换为该类型),那么你应该能够简单地调用它的Add或Insert方法。
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
}
}
}
我不明白什么是绑定源,在按钮点击时我应该添加一行。
答案 0 :(得分:1)
我在您的代码中看到了两个问题。
1)在xaml页面后面的代码中,将加载操作的结果分配给datagrid的ItemsSource。
dataGrid1.ItemsSource = operation.Entities;
问题在于operation.Entities
属于IEnumerable<TEntity>
类型,因此没有Add-method。我建议定义一个ObservableCollection并将其用作ItemsSource,并在Load-operation的回调方法中填充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定义Insert或Update方法。 我对你使用Class1和ProductPM感到有些困惑。它们应该是一样的吗?
无论如何,如果你想添加Class1的新实例,你需要在DomainService中使用InsertClass1(Class1 newObject)
方法(最好也是Update方法)。