简体   繁体   English

如何将GridView绑定到表具有外键

[英]how to bind a gridview to a table has a foreign key

i have table that has 3 foregin key now i want show that in a gridview, the class of table: 我现在有3个foregin键的表,我想在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; }

and that is the code for bind to grid view 这是绑定到网格视图的代码

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;
    }

for example methode of getall company: 例如getall公司的methode:

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

and when i run this code gridview shown classLibrary.classes.shift fro shift field what i have to do show the currect value in gridview. 当我运行此代码时,gridview显示了classLibrary.classes.shift shift字段中所要做的事情显示了gridview中的当前值。 i have use winforms and 我有使用winforms和

它使用每个类的重写toString()方法解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在具有外键的表中插入数据 - How to Insert Data in the table that has foreign key 如何将哈希表绑定到gridview - how to bind hash table to gridview 如何在GridView中显示外键的值? - How to display value of foreign key in GridView? 如何在WPF MVVM中使用外键绑定组合框 - How to bind a combobox with Foreign Key in WPF MVVM 如何使用表适配器与ASP GridView绑定? - how to use table adapter to bind with asp gridview? 如何:将数据集的每个表绑定到GridView的页面上 - How to: Bind Each Table of a DataSet to a Page on a GridView 如何创建具有复合键的表,其中每个成员都是其他表的外键成员 - How do I create a table that has a composite key each member of which is a foreign key member to other tables 我如何播种具有外键的表,该表在模型中被指定为对象,但在表中被指定为GUID - How do i seed a table that has a foreign key, which is specified as an object in the model, but as a guid in the table 如果表中有外键,如何在表中插入信息? - How do I insert information in a SQL table if the table has a foreign key in it? 如何在我的数据库中插入具有外键的行而不在链接表中添加行? - How can I insert a row in my database that has a foreign key without adding a row in the linked table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM