1

我有一个 LINQ 查询,如下所示:

    allRecords = (From metaset In section.sets
                  From metacell In metaset.cells
                  Let attrib = metacell.attrib
                  Order By attrib.attrib_name
                  Select Attribute = attrib.attrib_name,
                         Type = attrib.attrib_name,
                         Col = metacell.column,
                         Row = metacell.row
                     ).ToArray()

它返回一个<anonymous type>数组。
当我写时myDataGridView.DataSource = allRecords,网格会正确填充。

但是,如果我修改查询以返回对象数组CellDetail,而不是<anonymous type>

    allRecords = (From metaset In section.sets
                  From metacell In metaset.cells
                  Let attrib = metacell.attrib
                  Order By attrib.attrib_name
                  Select New CellDetail With {
                         .Attribute = attrib.attrib_name,
                         .Type = attrib.attrib_name,
                         .Col = metacell.column,
                         .Row = metacell.row
                     ).ToArray()

哪里CellDetail是这么定义的:

Public Class CellDetail

    Public Attribute As String
    Public Type As String
    Public Col As Int32
    Public Row As Int32

End Class

DataGridView 没有找到兼容的数据并且什么也不显示!

我应该实现一个 .Net 接口以使CellDetail类与 DataGridView 的 DataSource 兼容吗?

4

1 回答 1

3

DataGridView绑定到属性,而不是字段。使它们成为属性。

Public Class CellDetail    
    Public Property Attribute As String
    Public Property Type As String
    Public Property Col As Int32
    Public Property Row As Int32    
End Class

或者在 C# 中,这将是:

public class CellDetail {    
    public string Attribute {get;set;}
    public string Type {get;set;}
    public int Col {get;set;}
    public int Row {get;set;}
}
于 2012-10-24T10:04:11.863 回答