发布于 2022-01-04 06:13:24
您可以使用DataGridView组合框来完成此操作。您可能需要调整下面的一些建议,因为您没有发布任何代码
假设我们有病人和药物,而病人有。MedicineId给他们吃的药。实际上,这可能有很多:许多关系(许多病人服用相同的药物,同样的病人服用了许多药物),但DGV不能显示这种情况,为此您必须重复行,这是一种不同的技术--让我们现在有一个显示病人和药物的DGV,然后您可以扩展它以显示该病人以后的多行
所以你的DGV显示了你的病人的身份,名字,药物:
DGV.DataSource = context.Patients
.Where(p => p.Name == "John")
.Select(p => new PatientViewModel(p.Id, p.Name, p.MedicineId))
.ToList()在DGV中添加另一列:
var dgvcbc = new DataGridViewComboBoxColumn();
dgvcbc.HeaderText = "Medicine";
dgvcbc.DataMember = "MedicineId"; //the name of the column in the patients table
dgvcbc.ValueMember = "Val"; // the name of the property representing the medicine Id in the anonymous type below
dgvcbc.DisplayMember = "Disp"; // the name of the property in the AT below, representing the text to show in the combo
dgvcbc.DataSource = context.Medicines
.Select(m => new { Val = m.Id, Disp = m.Name } )
.ToList(); //a query that generates a key value pair into an AT for use with Display/Value newbies above组合体将从病人处读取药物Id,并查找要显示的文本。如果您更改组合框(选择另一项),则组合框将将新药Id推入患者列表--这是双向“绑定”。
https://stackoverflow.com/questions/70573339
复制相似问题