首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Csharp Datagrid和Entity Framework中显示外键值

如何在Csharp Datagrid和Entity Framework中显示外键值
EN

Stack Overflow用户
提问于 2019-02-26 20:18:51
回答 1查看 370关注 0票数 0

我有我的课表和学期表。每个学期都会有一个使用外键链接的会话,然而,我的datagrid只显示外键的整数值,我在网上尝试了几个答案来解决这个问题,但仍然不起作用

这是我的模型

代码语言:javascript
复制
  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; }
    }

我像这样填充了我的数据网格

代码语言:javascript
复制
void PopulateGridView()
        {
            using (dmsEntities db = new dmsEntities())
            {
                semesterGrid.AutoGenerateColumns = false;
                semesterGrid.DataSource = db.semesters
                    .Include(x => x.session)
                    .ToList();
                //semesterGrid.DataSource = db.semesters.ToList();
            }
        }

标记下的网格

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-02 02:22:41

最终,我解决了这个问题..我已经尝试了上面的建议,但仍然不能让它工作。我决定去Linq

代码语言:javascript
复制
 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; 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54885433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档