如何在c#中绑定网格视图

本文关键字:绑定 网格 视图 | 更新日期: 2023-09-27 18:02:16

我有gridview,我想从数据库绑定它,但它不绑定一些单元格。表的类:

public class Break
{
    #region Property
    [Key]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Column("FldKeyId")]
    public int MyKeyId { get; set; }
    [Required]
    public Machine Machine { get; set; }  
    [Required]
    public Employee Employee { get; set; }
    [Required]
    public 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; }

这是bind

的代码
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 = "MyName";
        dgvcolshift.DataSource = Shift.GetAllShift();
        dgvcolshift.DisplayMember = "MyName";
        dgvcolshift.ValueMember = "MyKeyId";
        dgvcolshift.DataPropertyName = "Shift";
        _Breaks=Break.GetAllBreak();
        dgvBreak.AutoGenerateColumns = false;
        dgvBreak.DataSource = _Breaks;
    }

这是getAllBreaks

的代码
 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();
    }

现在当它在shift单元格中运行时,显示classLibrary.classes.shift为机器,员工,如shift我该怎么做呢?

如何在c#中绑定网格视图

通过每个类的override toString方法解决