简体   繁体   English

如何在 DataGridView 中显示 LiteDB 数据?

[英]How to display LiteDB data in DataGridView?

I am new to LiteDB, however it seems perfect for my application after using a cumbersome CSV file, but I am having trouble displaying the data in a Windows Form DataGridView.我是 LiteDB 的新手,但是在使用繁琐的 CSV 文件后,它似乎非常适合我的应用程序,但是我无法在 Windows 窗体 DataGridView 中显示数据。

My LiteDB database is working fine with storing data but need help displaying the data.我的 LiteDB 数据库在存储数据方面工作正常,但需要帮助显示数据。

Here is my POCO and user data store method:这是我的 POCO 和用户数据存储方法:

public class Preset
{
   [BsonId]
   public int _id { get; set; }
   public string Name { get; set; }
   public int X { get; set; }
   public int Y { get; set; }
   public int Z { get; set; }
   public int Foam { get; set; }
}

public void DataBase()
{
   using (var db = new LiteDatabase(DB))
   {
      var col = db.GetCollection<Preset>("presets");    
      var preset = new Preset
      {
         Name = CustomPresetName.Text,
         X = Int16.Parse(CustomX.Text),
         Y = Int16.Parse(CustomY.Text),
         Z = Int16.Parse(CustomZ.Text),
         Foam = Int16.Parse(Foam.Text)
       };
       col.Insert(preset);
    }
}

I have simply tried linking the DataGridView with the object as a data source however it doesn't show the data, just the headers.我只是尝试将 DataGridView 与作为数据源的对象链接起来,但是它不显示数据,只显示标题。 I have also surfed the internet for a similar problem or answer but it seems to be a fairly untouched.我也在互联网上浏览过类似的问题或答案,但似乎还没有受到影响。

Any help on getting the data to display in the DataGridView is appreciated.任何有关让数据显示在 DataGridView 中的帮助表示赞赏。

PresetView.DataSource = col.FindAll().ToList()

请注意,自动排序不适用于此绑定方法。

private List<Preset> GetAll()
{
    var list = new List<Preset>();
    using (var db = new LiteDatabase(DB))
    {
        var col = db.GetCollection<Preset>("presets");
        foreach (Preset _id in col.FindAll())
        {
            list.Add(_id);
        }
    }
    return list;
}

public void DisplayPresetData()
{
    PresetView.DataSource = GetAll();
}

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

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