Binding LINQ query to DataGridView

前端 未结 2 698
失恋的感觉
失恋的感觉 2020-12-09 12:15

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          


        
相关标签:
2条回答
  • 2020-12-09 12:50

    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.

    0 讨论(0)
  • 2020-12-09 12:55

    just simply convert the result to a list and bind it to your grid.

    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
        }.ToList();
    dataGridView1.DataSource = query;
    
    0 讨论(0)
提交回复
热议问题