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

问题描述 投票:0回答:1

我把我的桌子作为课程和学期。每个学期总有一个会话,我用外键链接,但是,我的数据网格只显示外键的整数值,我在线尝试了几个答案来解决这个问题,但仍然无法正常工作

这是我的模特

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

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

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

标记的网格

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();
c# winforms datagrid entity-framework-6 foreign-keys
1个回答
0
投票

最终,我能够解决这个问题。在尝试了上面的建议后仍然无法使其正常工作。我决定去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; 
© www.soinside.com 2019 - 2024. All rights reserved.