DevExpress.XtraGrid.GridControl绑定数据源时的问题
最近用DevExpress.XtraGrid.GridControl+Linq To SQL实现一个列表时,发现显示数据特别慢,输出Log后发现在对DevExpress.XtraGrid.GridControl.DataSource赋值时,会访问到数据源的某个IList属性,部分测试代码如下:
this.gridControl1.ShowOnlyPredefinedDetails = true;
Northwind db = new Northwind();
db.Log = Console.Out;
var LondonCustomers =
from cust in db.Customers
where cust.City == "London"
select cust;
this.gridControl1.DataSource = LondonCustomers;
在执行最后一条语句时,除了访问绑定的每个Customer的public属性,之前还会访问到Customer.Orders,导致不必要的数据库读取耗时。
未接触过Linq的朋友也可以看下面的示例:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Data.Linq;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
A a1 = new A();
a1.Key = "a1key";
a1.Name = "a1name";
A a2 = new A();
a2.Key = "a2key";
a2.Name = "a2name";
List<A> list = new List<A>();
list.Add(a1);
list.Add(a2);
this.gridControl1.DataSource = list;
}
private class A
{
private List<B> bs1 = null;
public List<B> Bs1
{
get{return bs1;}
set{bs1 = value;}
}
private List<B> bs2 = null;
public List<B> Bs2
{
get{return bs2;}
set{bs2 = value;}
}
private string key=string.Empty;
public string Key
{
get{return key;}
set{key = value;}
}
private string name = string.Empty;
public string Name
{
get{return name;}
set{name = value;}
}
}
private class B
{
private string name = string.Empty;
public string Name
{
get{return name;}
set{name = value;}
}
}
}
}
在DevExpress.XtraGrid.GridControl显示每个A对象时,还会访问每个A对象的Bs1属性,但不会访问Bs2属性。
有没有大虾能够指点一下呢?不胜感激!