如何将gridview绑定到表有一个外键

本文关键字:有一个 绑定 gridview | 更新日期: 2023-09-27 18:02:48

我有一个表,有3个外键现在我想在gridview中显示,表的类:

[Key]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("FldKeyId")]
    public int MyKeyId { get; set; }
    [Required]
    public virtual Machine Machine { get; set; }  
    [Required]
    public virtual Employee Employee { get; set; }
    [Required]
    public virtual Shift Shift { get; set; }
    [Required]
    [Column ("FldStartTime")]
    public DateTime MyStartTime { get; set; }
    [Required]
    [Column ("FldEndTime")]
    public DateTime MyEndTime { get; set; }
    [Column("FldModificationDate")]
    [Required]
    public DateTime ModificationDate { get; set; }

,这是绑定到网格视图的代码

void RefreshBreakGrid()
    {
        dgvcolDate.DataPropertyName = "ModificationDate";
        dgvcolstarttime.DataPropertyName = "MyStartTime";
        dgvcolendtime.DataPropertyName = "MyEndTime";
        dgvcolemployee.DataSource = Employee.GetAllEmployee();
        dgvcolemployee.DisplayMember = "MyName";
        dgvcolemployee.ValueMember = "MyKeyId";
        dgvcolemployee.DataPropertyName = "Employee";

        dgvcolmachine.DataSource = Machine.GetAllMachine();
        dgvcolmachine.DisplayMember = "MyName";
        dgvcolmachine.ValueMember = "MyKeyId";
        dgvcolmachine.DataPropertyName = "Machine";
        dgvcolshift.DataSource = Shift.GetAllShift();
        dgvcolshift.DisplayMember = "MyName";
        dgvcolshift.ValueMember = "MyKeyId";
        dgvcolshift.DataPropertyName = "Shift";
        _Breaks=Break.GetAllBreak();
        dgvBreak.AutoGenerateColumns = false;
        dgvBreak.DataSource = _Breaks;
    }

举例方法:

public static List<Break> GetAllBreak()
{
    return new ContexManager().Breaks.Where(c => c.MyKeyId != 0).Include(c => c.Machine).Include(c => c.Employee).Include(c => c.Shift).ToList();
}

,当我运行这个代码gridview显示classLibrary.classes.shift从移位字段我要做的是显示gridview中的当前值。我用过winforms和

如何将gridview绑定到表有一个外键

用重写每个类的toString()方法求解