C#中如何将DataGridView选中行数据传递至Windows Form文本框
Windows Form子窗体DataGridView选中行填充主窗体文本框解决方案
这里提供三种实用的实现方案,解决子窗体选中行数据无法传递到主窗体的问题:
方法一:构造函数传递主窗体引用
通过在子窗体构造函数中接收主窗体实例,直接完成数据赋值操作。
主窗体代码(打开子窗体逻辑)
private void btnOpenChild_Click(object sender, EventArgs e) { // 传递当前主窗体实例给子窗体 PatientSelectForm childForm = new PatientSelectForm(this); childForm.Show(); }
子窗体代码
// 保存主窗体实例 private MainForm _mainForm; // 重载构造函数接收主窗体 public PatientSelectForm(MainForm mainForm) { InitializeComponent(); _mainForm = mainForm; } // DataGridView行选中事件(示例用CellClick) private void dgvPatients_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) return; DataGridViewRow selectedRow = dgvPatients.Rows[e.RowIndex]; // 直接给主窗体的文本框赋值,替换成你的控件名和列名 _mainForm.txtPacienteID.Text = selectedRow.Cells["PatientID"].Value.ToString(); _mainForm.txtPatientName.Text = selectedRow.Cells["PatientName"].Value.ToString(); _mainForm.txtAge.Text = selectedRow.Cells["Age"].Value.ToString(); }
规范优化建议:主窗体尽量避免直接暴露控件,可通过公共属性封装:
public string PacienteID { get => txtPacienteID.Text; set => txtPacienteID.Text = value; }
子窗体赋值时改用_mainForm.PacienteID = ...即可。
方法二:事件委托实现解耦传递
通过自定义事件,子窗体选中行时触发事件,主窗体订阅事件接收数据,符合面向对象解耦原则。
子窗体代码
// 定义自定义事件参数,封装需要传递的患者数据 public class PatientSelectedArgs : EventArgs { public string ID { get; set; } public string Name { get; set; } public string Age { get; set; } } // 声明事件 public event EventHandler<PatientSelectedArgs> PatientSelected; // 触发事件的私有方法 private void RaisePatientSelected(PatientSelectedArgs args) { PatientSelected?.Invoke(this, args); } // DataGridView选中事件 private void dgvPatients_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) return; DataGridViewRow row = dgvPatients.Rows[e.RowIndex]; var args = new PatientSelectedArgs { ID = row.Cells["PatientID"].Value.ToString(), Name = row.Cells["PatientName"].Value.ToString(), Age = row.Cells["Age"].Value.ToString() }; // 触发事件 RaisePatientSelected(args); }
主窗体代码
private void btnOpenChild_Click(object sender, EventArgs e) { PatientSelectForm childForm = new PatientSelectForm(); // 订阅子窗体的事件 childForm.PatientSelected += ChildForm_PatientSelected; childForm.Show(); } // 事件处理方法,填充主窗体文本框 private void ChildForm_PatientSelected(object sender, PatientSelectedArgs e) { txtPacienteID.Text = e.ID; txtPatientName.Text = e.Name; txtAge.Text = e.Age; }
方法三:模态窗体返回选中数据
如果子窗体是弹窗场景(模态),可通过ShowDialog()返回结果,结合子窗体公共属性获取选中数据。
子窗体代码
// 定义存储患者数据的类 public class PatientInfo { public string ID { get; set; } public string Name { get; set; } public string Age { get; set; } } // 公共属性,保存选中的患者数据 public PatientInfo SelectedPatient { get; private set; } // 双击行确认选择(也可添加确认按钮触发) private void dgvPatients_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) return; DataGridViewRow row = dgvPatients.Rows[e.RowIndex]; SelectedPatient = new PatientInfo { ID = row.Cells["PatientID"].Value.ToString(), Name = row.Cells["PatientName"].Value.ToString(), Age = row.Cells["Age"].Value.ToString() }; // 设置对话框结果,关闭子窗体 this.DialogResult = DialogResult.OK; this.Close(); }
主窗体代码
private void btnOpenChild_Click(object sender, EventArgs e) { PatientSelectForm childForm = new PatientSelectForm(); if (childForm.ShowDialog() == DialogResult.OK) { // 从子窗体属性获取数据并填充 txtPacienteID.Text = childForm.SelectedPatient.ID; txtPatientName.Text = childForm.SelectedPatient.Name; txtAge.Text = childForm.SelectedPatient.Age; } }
方案选择建议
- 快速实现选方法一,适合小型项目;
- 追求代码解耦、可维护性选方法二,适合中大型项目;
- 子窗体是弹窗场景选方法三,交互更友好。
内容的提问来源于stack exchange,提问作者Wilson Bienvenido Otaño Mateo
相关产品推荐
相关产品推荐

