我做了兩個表,第一個存盤員工的姓名,第二個存盤職責和日期。但我無法顯示該員工的姓名、分配給他的職責以及日期。這是我的代碼,希望你能幫助我。
public class MyTabels
{
[Table("Employee")]
public class Employee
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
[OneToMany]
public List<Duty> Duties { get; set; }
}
[Table("Duties")]
public class Duty
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Description { get; set; }
public DateTime Deadline { get; set; }
[ForeignKey(typeof(Employee))]
public int EmployeeId { get; set; }
}
}
這個 XAML 有一個 CollectionView,我在其中添加了專案:
<CollectionView x:Name="CV" IsGrouped="True">
<CollectionView.HeaderTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Name}" FontSize="Large"/>
<Label Text="{Binding LastName}" FontSize="Large"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</CollectionView.HeaderTemplate>
<CollectionView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Description}" FontSize="Medium"/>
<Label Text="{Binding Deadline}" FontSize="Medium"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
這是我的代碼:
var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Mydb.db");
var db = new SQLiteConnection(dbpath);
db.CreateTable<Employee>();
db.CreateTable<Duty>();
var employee = new Employee
{
Name = "Andrew",
LastName = "Programmer"
};
db.Insert(employee);
var duty1 = new Duty()
{
Description = "Project A Management",
Deadline = new DateTime(2017, 10, 31)
};
var duty2 = new Duty()
{
Description = "Reporting work time",
Deadline = new DateTime(2022, 12, 31)
};
db.Insert(duty1);
db.Insert(duty2);
employee.Duties = new List<Duty> { duty1, duty2 };
db.UpdateWithChildren(employee);
var employeeStored = db.GetWithChildren<Employee>(employee.Id);
CV.ItemsSource = employeeStored.Duties;
uj5u.com熱心網友回復:
創建一個新類來轉換您從資料庫中獲取的資料與 CollectionView ItemSource 匹配。
模型:
public class EmployeeList : List<Duty>
{
public string Name { get; set; }
public string LastName { get; set; }
public EmployeeList(string name, string lastName, List<Duty> duties) : base(duties)
{
Name = name;
LastName = lastName;
}
}
用法:使用 GetAllWithChildren 將所有員工從資料庫中獲取到串列中。
public partial class Page3 : ContentPage
{
public List<EmployeeList> employeeStored { get; set; } = new List<EmployeeList>();
public Page3()
{
InitializeComponent();
var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Mydb.db");
var db = new SQLiteConnection(dbpath);
db.CreateTable<Employee>();
db.CreateTable<Duty>();
var employee = new Employee
{
Name = "Andrew",
LastName = "Programmer"
};
db.Insert(employee);
var duty1 = new Duty()
{
Description = "Project A Management",
Deadline = new DateTime(2017, 10, 31)
};
var duty2 = new Duty()
{
Description = "Reporting work time",
Deadline = new DateTime(2022, 12, 31)
};
db.Insert(duty1);
db.Insert(duty2);
employee.Duties = new List<Duty> { duty1, duty2 };
db.UpdateWithChildren(employee);
//var employeeStored2 = db.GetWithChildren<Employee>(employee.Id);
var employeeStored = db.GetAllWithChildren<Employee>();
var list = Convert(employeeStored);
CV.ItemsSource = list;
this.BindingContext = this;
}
public List<EmployeeList> Convert(List<Employee> employees)
{
foreach (var item in employees)
{
employeeStored.Add(new EmployeeList(item.Name, item.LastName, item.Duties));
}
return employeeStored;
}
}
請注意, CollectionView 沒有單元格的概念。所以在你的 Xaml 中洗掉它。
更新:
Xml:
<CollectionView x:Name="CV" IsGrouped="True">
<CollectionView.GroupHeaderTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding Name}" FontSize="Large"/>
<Label Text="{Binding LastName}" FontSize="Large"/>
</StackLayout>
</DataTemplate>
</CollectionView.GroupHeaderTemplate>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding Description}" FontSize="Medium"/>
<Label Text="{Binding Deadline}" FontSize="Medium"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/360097.html
上一篇:排名資料庫元素python
下一篇:SQL聚合函式的執行順序
