绑定的datagridview到多个表的LINQ到SQL查询 [英] Binding datagridview to a multiple table linq-to-sql query
问题描述
我绑定LINQ到SQL查询来DataGridView的结果。如果我只是从单个数据库表中选择这工作得很好。但是,如果它是一个连接查询在那里我从两个表中选择字段,然后因为选择是进入一个匿名类型,结果是只读的。因此,我的datagridview也是只读的。我想在DGV的字段可编辑。
I'm binding the results of a linq-to-sql query to a datagridview. This works fine if I'm just selecting from a single database table. However, if it's a join query where I'm selecting fields from both tables, then because the select is going into an anonymous type, the results are readonly. Hence my datagridview is also readonly. I want the fields in the DGV to be editable.
如果我创建一个非匿名类型,并使用在LINQ查询,那么DGV是可编辑的,但调用的SubmitChanges()方法不起作用。我知道在结果调用的SubmitChanges()之前,我可以手工填写,但感觉应该有这样做的更好的方法。特别是考虑到它必须是一个相当普遍的任务。
If I create a non-anonymous type, and use that in the linq query, then the DGV is editable, but calling the SubmitChanges() method doesn't work. I know I could manually fill in the results before calling SubmitChanges(), but it feels like there should be a better way of doing this. Especially given that it must be a fairly common task.
什么是做这个?
三江源的推荐方法任何帮助,结果
丹
Thankyou for any help,
Dan.
推荐答案
我所做的就是创建一个显示类,以在LINQ to SQL的表对象作为参数构造函数,然后就包装我想显示的属性。例如,下面的类,我想允许编辑街道和城市,同时也显示应用程序的数量和状态:
What I've done is create a display class, taking the linq to sql table objects as arguments to the constructor and then just wrapping the properties I wanted to display. For example, the following class where I wanted to allow edits to the street and city, but also display the application number and status:
public class AddressDisplay
{
private Retailer _retailer;
private BusinessAddress _address;
public AddressDisplay(Retailer retailer, BusinessAddress address)
{
_retailer = retailer;
_address = address;
}
public string ApplicationNumber
{
get { return _retailer.ApplicationNumber; }
}
public string Status
{
get { return _retailer.Status; }
}
public string Street
{
get { return _address.Street1; }
set { _address.Street1 = value; }
}
public string City
{
get { return _address.City; }
set { _address.City = value; }
}
}
然后返回 AddressDisplay
绑定到 DataGridView的
:
var addresses = from a in _context.BusinessAddresses
join r in _context.Retailers on a.ApplicationNumber equals r.ApplicationNumber
where a.City == city
select new AddressDisplay(r, a);
心连心
HTH
这篇关于绑定的datagridview到多个表的LINQ到SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!