You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# 基于ID匹配填充患者表选中行关联人口统计表数据的实现咨询

实现方案

步骤1:为Demographics窗体添加接收参数的构造函数

在Demographics窗体的代码文件中新增字段存储选中患者ID,新增带参构造函数接收传值,窗体加载时执行关联查询:

public partial class Demographics : Form
{
    // 存储传入的选中患者ID
    private readonly int _selectedPatientId;
    // 保留原无参构造函数,保证窗体设计器正常加载
    public Demographics()
    {
        InitializeComponent();
    }
    // 新增带参构造函数接收患者ID
    public Demographics(int patientId) : this()
    {
        _selectedPatientId = patientId;
    }
    private void Demographics_Load(object sender, EventArgs e)
    {
        // 关联查询Patients和Demographics表,仅返回当前选中ID的匹配记录
        var demoRecords = _db.Patients
            .Join(_db.Demographics,
                p => p.PatientID,
                d => d.C_PatientID, // Demographics表关联的患者ID字段
                (p, d) => new
                {
                    // 按需提取两个表的字段即可
                    p.PatientFirstName,
                    p.PatientLastName,
                    p.DateOfBirth,
                    d.OtherDemographicsField // 替换为Demographics表实际字段名
                })
            .Where(x => x.PatientID == _selectedPatientId)
            .ToList();
        // 绑定查询结果到Demographics窗体的控件
        gvDemographics.DataSource = demoRecords;
    }
}

步骤2:修改患者列表窗体的按钮点击事件,传递选中行ID

修改btnViewMedicalHistory_Click逻辑,先获取选中行的患者ID再传入Demographics窗体:

private void btnViewMedicalHistory_Click(object sender, EventArgs e)
{
    // 校验是否选中行
    if(gvPatients.SelectedRows.Count == 0)
    {
        MessageBox.Show("请先选中一位患者记录");
        return;
    }
    // 从选中行提取PatientID(绑定列名为PatientNumber)
    int selectedPatientId = (int)gvPatients.SelectedRows[0].Cells["PatientNumber"].Value;
    // 传递ID打开Demographics窗体
    var viewMedicalHistory = new Demographics(selectedPatientId);
    viewMedicalHistory.MdiParent = this.MdiParent;
    viewMedicalHistory.Show();
}

可选优化配置

将患者列表控件gvPatients的SelectionMode属性设为FullRowSelect、MultiSelect属性设为false,避免用户选中单个单元格或多条记录的异常情况。

内容的提问来源于stack exchange,提问作者MoAl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 10:45:06