【发布时间】:2019-02-26 12:18:50
【问题描述】:
我有我的表作为会话和学期。每学期总有一个会话,我使用外键链接,但是,我的数据网格只显示外键的整数值,我在网上尝试了几个答案来解决这个问题,但仍然无法正常工作
这是我的模型
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();
【问题讨论】:
-
您应该添加 datagrid 代码,但在我看来,您可能需要包含您的导航属性,因为它是虚拟的:
db.semesters.Include(s => s.Session).ToList(); -
@SteveGreene 正如建议的那样,我尝试了这个,但仍然没有用。 semiGrid.DataSource = db.semesters .Include(x => x.session).ToList();
-
需要查看网格标记。你绑定到什么属性?
-
我正在使用 windowsForm
-
@SteveGreene 我正在使用 PopulateGridView() 方法通过 DataSource 属性填充网格。但是,我在我的问题中包含了设计师标记
标签: c# winforms datagrid entity-framework-6 foreign-keys