简体   繁体   English

在datagridview中显示linq查询时出现问题

[英]Problem displaying a linq query in a datagridview

Hello: I am working on a C# project that reads an XML file and returns a list of lists. 您好:我正在研究一个C#项目,该项目读取XML文件并返回列表列表。 When I want to display a list I do this: 当我要显示列表时,请执行以下操作:

IEnumerable<Foo> myFooQuery = from t in myLists.SelectMany( l => l.bar)
                              orderby t.StartTime descending
                              select t;
dataGridView1.DataSource = myFooQuery.ToList();

My problem is that when I do it that way, you can't click on the column header to sort the datagridview. 我的问题是,当我这样做时,您无法单击列标题对datagridview进行排序。 I tried myFooQuery.AsQueryable(), but then nothing shows up in the datagridview even though the query count is what I expect. 我尝试了myFooQuery.AsQueryable(),但是即使查询计数是我期望的值,datagridview中也没有任何显示。 Am I just missing something obvious, or do I have to use .Tolist()? 我只是缺少明显的东西,还是必须使用.Tolist()?

You could try: 您可以尝试:

EnumerableRowCollection<DataRow> myFooQuery = from t in myLists.SelectMany( l => l.bar)
                              orderby t.StartTime descending
                              select t;

DataView myDataView = myFooQuery.AsDataView();

dataGridView1.DataSource = myDataView;

EDIT - commented out line 编辑-注释掉行

//dataGridView1.DataBind(); //dataGridView1.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM