DevExpress.XtraGrid.GridControl绑定数据源时的问题

ffjing 2009-10-13 03:44:47
最近用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属性。

有没有大虾能够指点一下呢?不胜感激!
...全文
1094 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
白鸽 2011-01-25
  • 打赏
  • 举报
回复
这个新控件我也在玩!就是没有玩太深。不是有Demo吗可以看看里面的示例有没有啊
qldsrx 2011-01-25
  • 打赏
  • 举报
回复
你在说什么啊,什么叫还会访问每个A对象的Bs1属性,但不会访问Bs2属性,明明都会访问,两个都会访问的未接触过Linq的人也不可能来回答你,所以你没必要瞎举例,我反而看不懂你要说明什么了。
显示数据特别慢是因为你直接将LINQ TO SQL的对象赋值给GridControl了,这样做是不对的。你应该使用ToList()方法转换后,缓存到本地再给它加载,这样可以大大提高速度。另外你如果不想要加载某个列,就不应该使用......select cust的方法返回全部列。
happyrain2010 2011-01-25
  • 打赏
  • 举报
回复
Linq有没有 懒加载呢
shicsheng 2011-01-25
  • 打赏
  • 举报
回复
学习,多写资料出来啊
ffjing 2009-10-16
  • 打赏
  • 举报
回复
人工顶一下,有没有人能帮忙啊!

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧