C#WPF和Sqlite:Datagrid在尝试读取Sqlite数据库表时显示最后一个表记录

本文关键字:Sqlite 数据库 最后一个 记录 读取 显示 Datagrid C#WPF | 更新日期: 2023-09-27 18:29:19

我不明白为什么我的数据网格显示来自Sqlite数据库的最后一个表记录。这是我写的代码。

    ObservableCollection<Activity> _activitiesList = new ObservableCollection<Activity>();

  while (reader.Read())
{
   _act.Name = reader.GetString(reader.GetOrdinal("Activity"));
   _act.Customer = reader.GetString(reader.GetOrdinal("ActivityCustomer"));
   _act.Activitytpe = reader.GetString(reader.GetOrdinal("ActivityNature"));
   string st = reader.GetString(reader.GetOrdinal("StartedAt"));
   _act.StartedAt = Convert.ToDateTime(st);
   string et = reader.GetString(reader.GetOrdinal("FinishedAt"));
   _act.EndedAt = Convert.ToDateTime(et);
   //string _ratio = reader.GetString(reader.GetOrdinal("ActivityRatio"));
   //_act.ActivityRatio = Convert.ToDouble(_ratio);
   _act.Notes = reader.GetString(reader.GetOrdinal("Notes"));
   string _hldy = reader.GetString(reader.GetOrdinal("Holiday"));
   _act.IsHoliday = Convert.ToBoolean(_hldy);
   _activitiesList.Add(_act);
}
ActivitiesDataGrid.ItemsSource=_activitiesList;

C#WPF和Sqlite:Datagrid在尝试读取Sqlite数据库表时显示最后一个表记录

在内部,而您必须实例化一个新的活动类:

while (reader.Read())
{ 
      Activity _act=new Activity();
      ....
}

您可以在while循环中为"Activity"类创建新的对象实例吗。

 ObservableCollection<Activity> _activitiesList = new ObservableCollection<Activity>();
**while (reader.Read())
            {
                ***_act = new Activity();***
                _act.Name = reader.GetString(reader.GetOrdinal("Activity"));
               ....
               ....
             }**