简体   繁体   English

C# 在新的 gridview 1-1 关系中显示选定的行数据

[英]C# Show selected row data in a new gridview 1-1 relationship

I'm creating a simple Patient App.我正在创建一个简单的患者应用程序。 1-1 relationship between patient and medical history record. 1-1 患者与病史记录的关系。 When the app is started the patient are displayed.当应用程序启动时,会显示患者。 When one patient is selected and the button is clicked, the medical history for that patient should be displayed.选择一名患者并单击按钮时,应显示该患者的病史。

My problem is all medical history is being displayed rather than that particular patient.我的问题是所有病史都被显示,而不是那个特定的病人。

          private void PopulateGrid()
    {
        var records = _db.GeneralMedicalHistories.Select(q => new
        {
            PatientNumber = q.GeneralMedicalHistoryID,
            MaritalStatus = q.MartialStatus,
            Education = q.Education,
            BloodType = q.BloodType,
            Pregenancies = q.Pregnancies,
            Tobacco = q.Tobacco,
            Alcohol = q.Alcohol,
            Drug = q.Drug,
            MedicalHistoryNotes = q.MedicalHistoryNotes,

        }).ToList();

        gvDemographics.DataSource = records;
        gvDemographics.Columns["PatientNumber"].HeaderText = "Patient#";
        gvDemographics.Columns["MaritalStatus"].HeaderText = "Marital Status";
        gvDemographics.Columns["BloodType"].HeaderText = "Blood Type";
        gvDemographics.Columns["MedicalHistoryNotes"].HeaderText = "Medical History Notes";
        //Hide the column for ID. Changed from the hard coded column value to the name, 
        // to make it more dynamic. 
        gvDemographics.Columns["PatientNumber"].Visible = true;
    }

    

在此处输入图像描述 在此处输入图像描述

You've selected the elements that you want to display in your gridview, but you have not set any sort of criteria for filtering the results of your query.您已选择要在 gridview 中显示的元素,但您尚未设置任何类型的标准来过滤查询结果。 To do this, first obtain the ID associated with the record that you want to display, I'll call it patientID .为此,首先获取与您要显示的记录关联的 ID,我将其命名为patientID Adding the .Where(PatientNumber = patientID) extension method to your query should filter your results to just the patient with that patientID ..Where(PatientNumber = patientID)扩展方法添加到您的查询应该将您的结果过滤到仅具有该patientID的患者。

.Where() documentation. .Where() 文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM