【问题标题】:How to display LiteDB data in DataGridView?如何在 DataGridView 中显示 LiteDB 数据?
【发布时间】:2018-08-30 01:49:54
【问题描述】:

我是 LiteDB 的新手,但在使用繁琐的 CSV 文件后,它似乎非常适合我的应用程序,但我无法在 Windows 窗体 DataGridView 中显示数据。

我的 LiteDB 数据库在存储数据方面工作正常,但需要帮助来显示数据。

这是我的 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);
    }
}

我只是尝试将 DataGridView 与对象作为数据源链接,但它不显示数据,只显示标题。我也上网浏览过类似的问题或答案,但似乎没有受到任何影响。

感谢任何有关让数据显示在 DataGridView 中的帮助。

【问题讨论】:

    标签: c# datagridview windows-forms-designer litedb


    【解决方案1】:
    PresetView.DataSource = col.FindAll().ToList()
    

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

    【讨论】:

      【解决方案2】:
      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();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多