简体   繁体   English

如何在Csharp Datagrid和实体框架中显示外键值

[英]How Can I Display Foreign Key value in Csharp Datagrid and Entity Framework

I have my tables as session and semester. 我有我的表作为会议和学期。 For every semester there is always a session which I linked using foreign key, however, my datagrid only displays the integer value of the foreign key, I have tried several answers online to solve this but still not working 对于每个学期,总是有一个使用外键链接的会话,但是,我的datagrid仅显示外键的整数值,我已经尝试过在线几个答案来解决此问题,但仍然无法正常工作

Here is my model 这是我的模特

  public partial class semester
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public semester()
        {
            this.documentstores = new HashSet<documentstore>();
        }

        public int id { get; set; }
        public int session_id { get; set; }
        public string semester_name { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<documentstore> documentstores { get; set; }


        public virtual session session { get; set; }
    }

and I populated my datagrid like this 然后像这样填充我的数据网格

void PopulateGridView()
        {
            using (dmsEntities db = new dmsEntities())
            {
                semesterGrid.AutoGenerateColumns = false;
                semesterGrid.DataSource = db.semesters
                    .Include(x => x.session)
                    .ToList();
                //semesterGrid.DataSource = db.semesters.ToList();
            }
        }

the grid underlying markup 网格底层标记

this.semesterGrid.AllowUserToDeleteRows = false;
            this.semesterGrid.AllowUserToResizeRows = false;
            this.semesterGrid.BackgroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            this.semesterGrid.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.semesterGrid.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
            this.semesterGrid.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(174)))), ((int)(((byte)(219)))));
            dataGridViewCellStyle1.Font = new System.Drawing.Font("Segoe UI", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            dataGridViewCellStyle1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            dataGridViewCellStyle1.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(198)))), ((int)(((byte)(247)))));
            dataGridViewCellStyle1.SelectionForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(17)))), ((int)(((byte)(17)))), ((int)(((byte)(17)))));
            dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            this.semesterGrid.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
            this.semesterGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.semesterGrid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.id,
            this.sessionid,
            this.semester_name});
            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            dataGridViewCellStyle2.Font = new System.Drawing.Font("Segoe UI", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            dataGridViewCellStyle2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(136)))), ((int)(((byte)(136)))), ((int)(((byte)(136)))));
            dataGridViewCellStyle2.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(198)))), ((int)(((byte)(247)))));
            dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(17)))), ((int)(((byte)(17)))), ((int)(((byte)(17)))));
            dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
            this.semesterGrid.DefaultCellStyle = dataGridViewCellStyle2;
            this.semesterGrid.EnableHeadersVisualStyles = false;
            this.semesterGrid.Font = new System.Drawing.Font("Segoe UI", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.semesterGrid.GridColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            this.semesterGrid.Location = new System.Drawing.Point(420, 52);
            this.semesterGrid.Name = "semesterGrid";
            this.semesterGrid.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
            dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            dataGridViewCellStyle3.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(174)))), ((int)(((byte)(219)))));
            dataGridViewCellStyle3.Font = new System.Drawing.Font("Segoe UI", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            dataGridViewCellStyle3.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            dataGridViewCellStyle3.SelectionBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(198)))), ((int)(((byte)(247)))));
            dataGridViewCellStyle3.SelectionForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(17)))), ((int)(((byte)(17)))), ((int)(((byte)(17)))));
            dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
            this.semesterGrid.RowHeadersDefaultCellStyle = dataGridViewCellStyle3;
            this.semesterGrid.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.semesterGrid.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.semesterGrid.Size = new System.Drawing.Size(542, 240);
            this.semesterGrid.TabIndex = 18;
            this.semesterGrid.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.semesterGrid_CellFormatting);
            this.semesterGrid.DoubleClick += new System.EventHandler(this.semesterGrid_DoubleClick);
            // 
            // id
            // 
            this.id.DataPropertyName = "id";
            this.id.HeaderText = "Id";
            this.id.Name = "id";
            this.id.ReadOnly = true;
            this.id.Visible = false;
            // 
            // sessionid
            // 
            this.sessionid.DataPropertyName = "session_id";
            this.sessionid.HeaderText = "Session Year";
            this.sessionid.Name = "sessionid";
            this.sessionid.ReadOnly = true;
            // 
            // semester_name
            // 
            this.semester_name.DataPropertyName = "semester_name";
            this.semester_name.HeaderText = "Semester Name";
            this.semester_name.Name = "semester_name";
            this.semester_name.ReadOnly = true;
            // 
            // UCSemester
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.semesterGrid);
            this.Controls.Add(this.sessionComboBox);
            this.Controls.Add(this.cancelButton);
            this.Controls.Add(this.saveSemesterButton);
            this.Controls.Add(this.semesterTextBox);
            this.Controls.Add(this.metroLabel3);
            this.Controls.Add(this.metroLabel2);
            this.Controls.Add(this.metroLabel1);
            this.Name = "UCSemester";
            this.Size = new System.Drawing.Size(1100, 452);
            this.Load += new System.EventHandler(this.UCSemester_Load);
            ((System.ComponentModel.ISupportInitialize)(this.semesterGrid)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

Eventually, I was able to solve this.. Having tried the suggestion above and still couldn't get it working. 最终,我能够解决此问题。尝试了以上建议,但仍然无法使它起作用。 I resolved to Linq 我决定去Linq

 var data = (from sem in db.semesters.ToList()
                            join ses in db.sessions
                            on sem.session_id equals ses.id
                            select new {
                                sem.id,
                                 Session = ses.session_name,
                                 Semester = sem.semester_name }).ToList();

                semesterGrid.DataSource = data; 

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

相关问题 如何使用Entity Framework的外键值显示 - How to display with Entity Framework value of a foreign key 如何在数据网格中显示外键数据 - How can I display foreign key data in a datagrid 如何在Entity Framework的外键字段中添加数据? - How can I add data in foreign key field in Entity Framework? 如何使用实体框架保存外键? - How can I save a foreign key using Entity Framework? 如何使用实体框架查询外键对象? - How can I query the foreign key objects using Entity Framework? 实体框架:如何更改实体中现有的整数外键值? - Entity Framework: how to change existing integer foreign key value in an Entity? 在WPF(MVVM / Entity Framework)中更新表单时,如何在数据库中显示外键数据值到TextBlock? - How to display foreign key data value from database to TextBlock on while update form in WPF (MVVM / Entity Framework)? WPF实体框架外键不会在DataGrid中加载其值 - WPF Entity Framework Foreign key won't load it's value in a DataGrid 使用实体框架时,为什么不能访问外键字段的值? - Why can't I access the value(s) of foreign key field(s) when using Entity Framework? 具有ObservableCollection的datagrid中的主键值(实体框架) - Primary Key value in datagrid with ObservableCollection (Entity Framework)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM