【发布时间】:2021-01-13 23:29:41
【问题描述】:
据我了解,ComboBox 列在 DataGridView 中的绑定比标准列更具动态性,并且这种灵活性可用于从二阶属性中使用 DisplayMembers。这种方法是hereMr. Aghaei第一次提到的。
但是,我没有做对。我的应用程序仍然抛出“名称”不存在的异常。
public void CreateEmployeeTable()
{
DataGridViewComboBoxColumn jobTitleColumn = new DataGridViewComboBoxColumn();
jobTitleColumn.HeaderText = "Job Title";
jobTitleColumn.DataPropertyName = "JobTitle";
jobTitleColumn.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
jobTitleColumn.DataPropertyName = "ID";
jobTitleColumn.DataSource = globalEmployeeList;
jobTitleColumn.ValueMember = "ID";
jobTitleColumn.DisplayMember = "Name";
jobTitleColumn.ReadOnly = true;
employeeGridView.AutoGenerateColumns = false;
employeeGridView.ColumnCount = 2;
employeeGridView.Columns[0].HeaderText = "Employee ID";
employeeGridView.Columns[0].DisplayIndex = 0;
employeeGridView.Columns[0].DataPropertyName = "ID";
employeeGridView.Columns[1].HeaderText = "Name";
employeeGridView.Columns[1].DataPropertyName = "ListView";
employeeGridView.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
employeeGridView.Columns.Add(jobTitleColumn);
employeeGridView.DataSource = globalEmployeeList;
}
这是类定义:
public class EmployeeModel
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Nickname { get; set; }
public DepartmentModel Department { get; set; }
public TitleModel JobTitle { get; set; }
public DateTime HireDate { get; set; }
public List<EmailModel> EmailList { get; set; } = new List<EmailModel>();
public List<PhoneModel> PhoneList { get; set; } = new List<PhoneModel>();
public List<RestrictionModel> RestrictionsList { get; set; } = new List<RestrictionModel>();
public List<CitationModel> CitationsList { get; set; } = new List<CitationModel>();
public List<CertificationModel> CertificationList { get; set; } = new List<CertificationModel>();
public string ListView
{
get
{
return $"{LastName}, {FirstName}";
}
}
public string ToEmailString()
{
IEnumerable<string> employeeEmailStrings = EmailList.Select(emmod => emmod.ToString());
string employeeEmailString = string.Join($"{Environment.NewLine}", employeeEmailStrings);
IEnumerable<string> certificationStrings = CertificationList.Select(clistmod => clistmod.ToString());
string certificationString = string.Join($"{Environment.NewLine}", certificationStrings);
IEnumerable<string> phoneStrings = PhoneList.Select(plistmod => plistmod.ToString());
string phoneString = string.Join($"{Environment.NewLine}", phoneStrings);
return $"{FirstName}, {LastName}: {Environment.NewLine} -{JobTitle.Name}- {Environment.NewLine} {employeeEmailString} {Environment.NewLine} {certificationString} {Environment.NewLine} {phoneString}";
}
public class EmailModel
{
public int ID { get; set; }
public string Address { get; set; }
public string Type { get; set; }
public override string ToString()
{
return $"{Address} ({Type})";
}
}
public class PhoneModel
{
public int ID { get; set; }
public string Number { get; set; }
public string Type { get; set; }
public override string ToString()
{
return $"{Number} ({Type})";
}
}
}
以及 TitleModel 的定义:
public class TitleModel
{
public string Name { get; set; }
public int ID { get; set; }
}
}
【问题讨论】:
标签: c# .net winforms datagridview