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

C# WinForm跨窗体访问DataGridView数据填充至TextBox实现方法

WinForm跨窗体选择医生数据回填实现方案

整体实现不破坏原有医生信息管理的增删改逻辑,仅通过新增属性、扩展事件即可完成选择回填需求,步骤如下:

1. 改造DoctorDetailsForm医生详情窗体

原有窗体已经实现医生数据加载、增删改逻辑,仅需要新增回传属性、选择模式判断即可复用为选择弹窗:

  • 首先在窗体类中新增3个公共成员,用于模式标记和选中值回传:
// 标记当前是否为「选择医生」弹窗模式,默认是普通增删改模式
public bool IsSelectMode { get; set; } = false;
// 回传选中的医生姓名
public string SelectedDoctorName { get; private set; }
// 回传选中的诊疗费用
public decimal SelectedDoctorFees { get; private set; }
  • 修改原有的dgvDoctorDetails_CellDoubleClick双击事件,兼容编辑和选择两种场景,同时修复原代码直接取SelectedRows[0]可能引发的索引报错问题:
private void dgvDoctorDetails_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    // 过滤双击表头的无效操作
    if (e.RowIndex < 0) return;
    DataGridViewRow selectedRow = dgvDoctorDetails.Rows[e.RowIndex];

    // 选择模式下直接回传值、关闭弹窗
    if (IsSelectMode)
    {
        SelectedDoctorName = selectedRow.Cells[1].Value?.ToString() ?? string.Empty;
        SelectedDoctorFees = Convert.ToDecimal(selectedRow.Cells[6].Value);
        DialogResult = DialogResult.OK;
        Close();
        return;
    }

    // 原有增删改场景的编辑逻辑完全保留
    btnSave.Enabled = false;
    Doctor_ID = Convert.ToInt32(selectedRow.Cells[0].Value);
    txtDoctorid.Text = selectedRow.Cells[0].Value.ToString();
    txtDoctorName.Text = selectedRow.Cells[1].Value.ToString();
    txtDoctorDegree.Text = selectedRow.Cells[2].Value.ToString();
    txtDoctorTiming.Text = selectedRow.Cells[3].Value.ToString();
    txtDoctorMobile.Text = selectedRow.Cells[4].Value.ToString();
    txtDoctorAddress.Text = selectedRow.Cells[5].Value.ToString();
    txtDoctroFees.Text = selectedRow.Cells[6].Value.ToString();
}

注意:如果需要在选择模式下隐藏增删改相关的编辑控件,需要在窗体设计器中把对应按钮、文本框的Modifiers属性设置为Public,否则外部窗体无法修改控件可见性。

2. 给Form1的Doctor输入框添加触发逻辑

找到Form1中用于输入医生姓名的文本框(参考示例控件名为txtDoctor,诊疗费输入框为txtFees,请和自身实际控件名匹配),为其添加Enter事件——该事件会在输入框获得焦点(光标定位到输入框)时触发:

private void txtDoctor_Enter(object sender, EventArgs e)
{
    // using块会自动释放窗体资源,避免内存泄漏
    using (DoctorDetailsForm doctorSelectForm = new DoctorDetailsForm())
    {
        // 初始化弹窗为选择模式
        doctorSelectForm.IsSelectMode = true;
        doctorSelectForm.StartPosition = FormStartPosition.CenterParent;
        // 可选:选择模式下隐藏编辑类控件,仅保留医生列表
        doctorSelectForm.btnSave.Visible = false;
        doctorSelectForm.btnUpdate.Visible = false;
        doctorSelectForm.btnClear.Visible = false;
        doctorSelectForm.txtDoctorid.Visible = false;
        doctorSelectForm.txtDoctorName.Visible = false;
        doctorSelectForm.txtDoctorDegree.Visible = false;
        doctorSelectForm.txtDoctorTiming.Visible = false;
        doctorSelectForm.txtDoctorMobile.Visible = false;
        doctorSelectForm.txtDoctorAddress.Visible = false;
        doctorSelectForm.txtDoctroFees.Visible = false;

        // 弹窗确认选中后自动回填值
        if (doctorSelectForm.ShowDialog(this) == DialogResult.OK)
        {
            txtDoctor.Text = doctorSelectForm.SelectedDoctorName;
            txtFees.Text = doctorSelectForm.SelectedDoctorFees.ToString();
            // 回填后自动跳到下一个输入框,优化录入效率
            txtFees.Focus();
        }
    }
}

参考界面效果

医生选择弹窗界面
数据录入主界面
医生列表界面

可选优化点

  • 可以在DoctorDetailsForm顶部增加搜索文本框,输入医生姓名快速过滤列表,减少找数据的时间
  • 如果不希望每次光标进入医生输入框都弹出选择窗,可以在输入框旁加一个选择按钮,仅点击按钮时弹出弹窗,避免用户修改已有医生姓名时被打断
  • 可以给DataGridView设置选择整行的属性:dgvDoctorDetails.SelectionMode = DataGridViewSelectionMode.FullRowSelect,双击选择时交互更清晰

内容的提问来源于stack exchange,提问作者Sheikh Muhammad Nawaz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 23:51:26