【问题标题】:Set result of query to DataGrid or ListView by SQLite.NET通过 SQLite.NET 将查询结果设置为 DataGrid 或 ListView
【发布时间】:2017-10-15 20:23:42
【问题描述】:

是否可以将查询结果直接设置为 DataGrid(不映射到对象)。我有一些动态报告,我只想在 Grid 中显示,不设置列等。

通常这不是问题,因为我将数据“映射”到模型中的对象,例如:

using (var db = new SQLiteConnection(new SQLite.Net.Platform.Generic.SQLitePlatformGeneric(), "zakupy.db"))
{
    listPerson = db.Table<Persons>().Where(x => x.Property == "P" && x.Status == 0).ToList();
}
lstPersons.DataContext = listPerson;

【问题讨论】:

  • 设置ItemsSourcedataGrid的属性(lstPersons.ItemsSource = listPerson;)
  • 是的,但在此示例中,“listPerson”是对象“Persons”的列表。我不知道如何简单地绑定 qyery ,例如“select * from Events”。是否可以不“映射”到对象?
  • 如果你有一些通用的 sql 查询(类似于"select * from Events")然后将结果加载到DataTable 并设置lstPersons.ItemsSource = myDataTableInstance.DefaultView;

标签: c# wpf sqlite sqlite-net-extensions sqlite.net


【解决方案1】:

好的,我就是这样想的:

 string ConString = "sale.db";
 string passedquery = "select * from Events";
        DataTable dt = new DataTable();
        using (SQLiteConnection con = new SQLiteConnection(ConString))
        {
            try
            {
                using (var statement = con.Prepare(passedquery))
                {
                    for (int i = 0; i < statement.ColumnCount; i++)
                    {
                        dt.Columns.Add(statement.ColumnName(i));
                    }
                    while (statement.Step() == SQLiteResult.ROW)
                    {

                        DataRow dr;
                        dr = dt.NewRow();

                        for (int i = 0; i < statement.ColumnCount; i++)
                        {
                            dr[i] = statement[i];
                        }
                        dt.Rows.Add(dr);
                    }
                }
            }

            catch(SQLitePCL.SQLiteException)
            {

                MessageBox.Show("Nieprawidłowe zapytanie");
            }             
        }
        dtgCustomReport.DataContext = dt.DefaultView;

但我仍然不知道这是不是最简单的方法:)

【讨论】:

    【解决方案2】:

    您需要以某种方式将“select * from Events”查询的结果映射到IEnumerable

    您当然可以使用SQLiteDataReader 并将匿名对象添加到ItemsSource。像这样的:

    System.Collections.ArrayList list = new System.Collections.ArrayList();
    using (var db = new SQLiteConnection(new SQLite.Net.Platform.Generic.SQLitePlatformGeneric(), "zakupy.db"))
    {
        db.Open();
        const string sql = "select * from Events";
        SQLiteCommand command = new SQLiteCommand(sql, db);
        SQLiteDataReader reader = command.ExecuteReader();
        while (reader.Read())
            list.Add(new { Name = reader["name"].ToString() });
    }
    lstPersons.ItemsSource = list;
    

    我真的不明白您为什么要这样做而不是绑定到通用 IEnumerable&lt;T&gt;

    无论如何,您可以将DataGridListViewItemsSource 设置或绑定到任何IEnumerable。就控件而言,您选择如何填充此序列并不重要。

    【讨论】:

    • 是的,但是在您的示例中,我仍然必须指定一些列,如下所示: list.Add(new { Name = reader["name"].ToString() } );还是我错了?
    • 是的,您需要创建实际对象并以某种方式设置属性。
    • 我已经添加了我的解决方案
    猜你喜欢
    • 2019-12-29
    • 2015-01-02
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多