如何收集18个Tab页中选中的RadioButton并写入数据库?
实现步骤
1. 编写通用方法获取选中的RadioButton值
因为RadioButton可能嵌套在GroupBox、Panel等容器控件中,需要递归遍历控件树来找到选中的项:
private string GetSelectedRadioButtonValue(Control parentControl) { foreach (Control ctrl in parentControl.Controls) { if (ctrl is RadioButton radioBtn && radioBtn.Checked) { // 返回RadioButton的Text或Tag属性,根据你的存储需求选择 return radioBtn.Text; } // 递归查找子控件中的RadioButton else if (ctrl.HasChildren) { string childValue = GetSelectedRadioButtonValue(ctrl); if (!string.IsNullOrEmpty(childValue)) return childValue; } } return null; // 无选中项时返回null }
2. 收集所有Tab页的选中数据到Profile对象
根据Tab页顺序与Profile类属性的对应关系,逐个获取每个Tab页的选中值:
private Profile CollectAllSelectedData() { Profile profile = new Profile(); TabControl tabControl = tabOrthpedic; // 按Tab页顺序对应Profile属性,根据你的实际页面顺序调整 profile.PainOrgan = GetSelectedRadioButtonValue(tabControl.TabPages[0]); profile.NeckLoc = GetSelectedRadioButtonValue(tabControl.TabPages[1]); profile.BackLoc = GetSelectedRadioButtonValue(tabControl.TabPages[2]); profile.LowBackLoc = GetSelectedRadioButtonValue(tabControl.TabPages[3]); profile.ShoulderLoc = GetSelectedRadioButtonValue(tabControl.TabPages[4]); profile.ElbowLoc = GetSelectedRadioButtonValue(tabControl.TabPages[5]); profile.HandLoc = GetSelectedRadioButtonValue(tabControl.TabPages[6]); profile.PelvicLoc = GetSelectedRadioButtonValue(tabControl.TabPages[7]); profile.KneeLoc = GetSelectedRadioButtonValue(tabControl.TabPages[8]); profile.FootLoc = GetSelectedRadioButtonValue(tabControl.TabPages[9]); profile.PainRate = GetSelectedRadioButtonValue(tabControl.TabPages[10]); profile.PainTime = GetSelectedRadioButtonValue(tabControl.TabPages[11]); profile.PainHistory = GetSelectedRadioButtonValue(tabControl.TabPages[12]); profile.PainResult = GetSelectedRadioButtonValue(tabControl.TabPages[13]); profile.Follow = GetSelectedRadioButtonValue(tabControl.TabPages[14]); profile.Expectation = GetSelectedRadioButtonValue(tabControl.TabPages[15]); profile.Limitation = GetSelectedRadioButtonValue(tabControl.TabPages[16]); profile.DesieseHistory = GetSelectedRadioButtonValue(tabControl.TabPages[17]); // ProfileNumber如果是手动输入,从对应的文本框获取 profile.ProfileNumber = txtProfileNumber.Text.Trim(); return profile; }
3. 实现数据库存储逻辑
以SQL Server为例,使用ADO.NET参数化查询(避免SQL注入)将Profile数据写入数据库:
private void SaveProfileToDb(Profile profile) { // 替换为你的数据库连接字符串 string connStr = "Data Source=你的服务器名;Initial Catalog=你的数据库名;Integrated Security=True"; string insertSql = @"INSERT INTO Profiles (ProfileNumber, PainOrgan, NeckLoc, BackLoc, LowBackLoc, ShoulderLoc, ElbowLoc, HandLoc, PelvicLoc, KneeLoc, FootLoc, PainRate, PainTime, PainHistory, PainResult, Follow, Expectation, Limitation, DesieseHistory) VALUES (@ProfileNumber, @PainOrgan, @NeckLoc, @BackLoc, @LowBackLoc, @ShoulderLoc, @ElbowLoc, @HandLoc, @PelvicLoc, @KneeLoc, @FootLoc, @PainRate, @PainTime, @PainHistory, @PainResult, @Follow, @Expectation, @Limitation, @DesieseHistory)"; try { using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand(insertSql, conn)) { // 添加参数,处理空值为DBNull.Value cmd.Parameters.AddWithValue("@ProfileNumber", string.IsNullOrEmpty(profile.ProfileNumber) ? DBNull.Value : (object)profile.ProfileNumber); cmd.Parameters.AddWithValue("@PainOrgan", profile.PainOrgan ?? DBNull.Value); cmd.Parameters.AddWithValue("@NeckLoc", profile.NeckLoc ?? DBNull.Value); cmd.Parameters.AddWithValue("@BackLoc", profile.BackLoc ?? DBNull.Value); cmd.Parameters.AddWithValue("@LowBackLoc", profile.LowBackLoc ?? DBNull.Value); cmd.Parameters.AddWithValue("@ShoulderLoc", profile.ShoulderLoc ?? DBNull.Value); cmd.Parameters.AddWithValue("@ElbowLoc", profile.ElbowLoc ?? DBNull.Value); cmd.Parameters.AddWithValue("@HandLoc", profile.HandLoc ?? DBNull.Value); cmd.Parameters.AddWithValue("@PelvicLoc", profile.PelvicLoc ?? DBNull.Value); cmd.Parameters.AddWithValue("@KneeLoc", profile.KneeLoc ?? DBNull.Value); cmd.Parameters.AddWithValue("@FootLoc", profile.FootLoc ?? DBNull.Value); cmd.Parameters.AddWithValue("@PainRate", profile.PainRate ?? DBNull.Value); cmd.Parameters.AddWithValue("@PainTime", profile.PainTime ?? DBNull.Value); cmd.Parameters.AddWithValue("@PainHistory", profile.PainHistory ?? DBNull.Value); cmd.Parameters.AddWithValue("@PainResult", profile.PainResult ?? DBNull.Value); cmd.Parameters.AddWithValue("@Follow", profile.Follow ?? DBNull.Value); cmd.Parameters.AddWithValue("@Expectation", profile.Expectation ?? DBNull.Value); cmd.Parameters.AddWithValue("@Limitation", profile.Limitation ?? DBNull.Value); cmd.Parameters.AddWithValue("@DesieseHistory", profile.DesieseHistory ?? DBNull.Value); conn.Open(); cmd.ExecuteNonQuery(); MessageBox.Show("数据保存成功"); } } } catch (Exception ex) { MessageBox.Show($"保存失败:{ex.Message}"); // 可添加日志记录逻辑 } }
4. 完善Tab页切换与提交逻辑
- 增强Tab切换验证:用户未完成当前页选择时不允许切换:
private void simpleButton2_Click(object sender, EventArgs e) { var currentTab = tabOrthpedic.SelectedTabPage; string selectedValue = GetSelectedRadioButtonValue(currentTab); if (string.IsNullOrEmpty(selectedValue)) { MessageBox.Show("请完成当前页面的选项选择"); return; } var tabIndex = tabOrthpedic.SelectedTabPageIndex; if (tabIndex == tabOrthpedic.TabPages.Count - 1) { return; } tabOrthpedic.SelectedTabPageIndex += 1; }
- 提交按钮点击事件:收集数据并保存到数据库:
private void btnSubmit_Click(object sender, EventArgs e) { Profile profile = CollectAllSelectedData(); // 可选:验证必填项 if (string.IsNullOrEmpty(profile.ProfileNumber)) { MessageBox.Show("请填写档案编号"); return; } if (string.IsNullOrEmpty(profile.PainOrgan)) { MessageBox.Show("请选择疼痛器官"); return; } // 其他必填项验证... SaveProfileToDb(profile); }
注意事项
- 确保数据库表
Profiles的字段与Profile类属性完全对应,数据类型匹配。 - 若使用其他数据库(如MySQL),只需替换对应的连接对象(
MySqlConnection、MySqlCommand)和连接字符串。 - 可根据需求调整RadioButton的返回值(比如用
Tag存储编码,Text显示名称)。
内容的提问来源于stack exchange,提问作者user19446287
相关产品推荐
相关产品推荐

