0

我正在使用 LiteDB。客户定义如下:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string[] Phones { get; set; }
    public string[] Cars { get; set; }
}

当我想在数据网格视图中显示我的数据时,只显示前两列,而数据网格视图不显示最后两列是数组。

这是GetAll

private List<Customer> GetAll()
    {
        var issuesToReturn = new List<Customer>();
        try
        {
            using (var db = new LiteDatabase(Constants.ConnectionString))
            {
                var issues = db.GetCollection<Customer>("customers");
                foreach (Customer issueItem in issues.FindAll())
                {
                    issuesToReturn.Add(issueItem);
                }
            }
        }
        catch (Exception exp)
        {
            MessageBox.Show(exp.Message);
        }
        return issuesToReturn;
    }
4

2 回答 2

1

您可以尝试使用另一个字段来格式化网格中的数据(但不存储在数据文件中)。像这样:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string[] Phones { get; set; }
    public string[] Cars { get; set; }

    [BsonIgnore]
    public string DisplayPhones => string.Join(", ", Phones);

    [BsonIgnore]
    public string DisplayCars => string.Join(", ", Cars);
}

然后,更改您的网格以使用此“显示”属性映射您的列。

于 2018-04-16T12:52:15.440 回答
0

使用 "Include" 方法加载相关数据:

var issues = db.GetCollection("customers").Include(x => x.Phones).Include(x => x.Cars);

另请注意,WPF 中的 GridView 不能单独在一个单元格中显示数组。您需要手动添加列:https ://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview(v=vs.110).aspx

于 2018-04-13T13:02:12.417 回答