将LINQ查询绑定到DataGridView [英] Binding LINQ query to DataGridView

查看:668
本文介绍了将LINQ查询绑定到DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是非常混乱的,我使用AsDataView将查询结果绑定到dgv,它可以正常工作如下:

  var query = from c in myDatabaseDataSet.Diamond其中c.p_Id == p_Id select c; 
dataGridView1.DataSource = query.AsDataView();

但是,这会导致错误:

  var query = from myDatabaseDataSet.Items中的项
其中item.p_Id == p_Id
在myDatabaseDataSet.Diamond中连接钻石
on item.p_Id等于diamond.p_Id
myDatabaseDataSet.DiamondCategory中的连接类别diamond bc_Id上的
等于category.dc_Id
选择新
{
Product = item.p_Name,
Weight = diamond.d_Weight,
Category = category.dc_Name
};

dataGridView1.DataSource = query.AsDataView();

错误:

 code>实例参数:无法从
'System.Collections.Generic.IEnumerable< AnonymousType#1>'转换为
'System.Data.DataTable'

AsDataView不会在查询中显示(List)。为什么会发生这种情况?如何将上面的查询绑定到dgv然后?

解决方案

AsDataView 如下:

  public static DataView AsDataView(
此DataTable表

唯一的参数是 DataTable 。 / p>

您的查询是返回一个 IEnumerable ,它不具有隐式转换为DataTable的匿名类型(或 DataRow 实例,在这种情况下,您可以使用它来帮助您创建一个DataTable)。



你需要得到结果回到一个DataTable或者你可以转换成一个DataTable的东西,然后它将工作。



在你的具体情况下,你似乎(或使用) a href =http://msdn.microsoft.com/en-us/library/esbykkzb(VS.71).aspx =nofollow noreferrer>键入的数据集。如果是这种情况,那么你应该能够获取所选择的值,然后创建新的类型化的DataRow实例(应该有工厂方法),然后将其放入一个类型化的DataTable中,该AsDataView可以被调用。


This is very confusing, I use AsDataView to bind query result to a dgv and it works fine with the following:

var query = from c in myDatabaseDataSet.Diamond where c.p_Id == p_Id select c;
dataGridView1.DataSource = query.AsDataView();

However, this one results in an Error:

var query = from item in myDatabaseDataSet.Items
    where item.p_Id == p_Id
    join diamond in myDatabaseDataSet.Diamond
        on item.p_Id equals diamond.p_Id
    join category in myDatabaseDataSet.DiamondCategory
        on diamond.dc_Id equals category.dc_Id
    select new
    {
        Product = item.p_Name,
        Weight = diamond.d_Weight,
        Category = category.dc_Name
    };

    dataGridView1.DataSource = query.AsDataView();

Error:

Instance argument: cannot convert from
'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 
'System.Data.DataTable'

AsDataView doesn't show up in query.(List). Why is this happen? How to bind the query above to the dgv then?.

解决方案

The signature of the AsDataView is as follows:

public static DataView AsDataView(
    this DataTable table
)

The only parameter is the DataTable.

The query you have is returning an IEnumerable of an anonymous type which doesn't have an implicit conversion to a DataTable (or a sequence of DataRow instances, in which case you could use that to help you create a DataTable).

You need to get the results back into a DataTable or something you can convert into a DataTable and then it will work.

In your particular case, it seems that you were (or are) using typed DataSets. If that is the case, then you should be able to take the values that were selected and then create new typed DataRow instances (there should be factory methods for you) which can then be put into a typed DataTable, which AsDataView can be called on.

这篇关于将LINQ查询绑定到DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆