C# 遍历窗体控件,初始化数据,并获得更新数据库代码

上传人:飞*** 文档编号:39929328 上传时间:2018-05-21 格式:DOCX 页数:9 大小:14.54KB
返回 下载 相关 举报
C# 遍历窗体控件,初始化数据,并获得更新数据库代码_第1页
第1页 / 共9页
C# 遍历窗体控件,初始化数据,并获得更新数据库代码_第2页
第2页 / 共9页
C# 遍历窗体控件,初始化数据,并获得更新数据库代码_第3页
第3页 / 共9页
C# 遍历窗体控件,初始化数据,并获得更新数据库代码_第4页
第4页 / 共9页
C# 遍历窗体控件,初始化数据,并获得更新数据库代码_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《C# 遍历窗体控件,初始化数据,并获得更新数据库代码》由会员分享,可在线阅读,更多相关《C# 遍历窗体控件,初始化数据,并获得更新数据库代码(9页珍藏版)》请在金锄头文库上搜索。

1、在 Winform 的工程中,如果我们不绑定数据源到窗体中,为每个控件自动赋值的话,那么一些操作将非常繁琐例如:1、新建项目,需要初始化窗体所有控件;2、保存更新项目,需要读取窗体所有控件等等。为此,写了个遍历窗体控件的类,需要窗体中所有控件和数据库字段名保持一致。using System;using System.Data;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Linq;using System.Text;using System.Windo

2、ws.Forms;namespace allControlSetpublic static class dbManager/为窗体所有输入赋值public static void setValue(Control.ControlCollection ctc, DataTable dt)foreach (Control ct in ctc) switch (ct.GetType().Name)case “TextBox“:TextBox tb = (TextBox)ct;tb.Text = dt.Rows0tb.Name.ToString();break;case “ComboBox“:Comb

3、oBox cb = (ComboBox)ct; cb.SelectedValue = dt.Rows0cb.Name.ToString();break;case “CheckBox“:CheckBox ck = (CheckBox)ct;ck.Checked = Boolean.Parse(dt.Rows0ck.Name.ToString();break;default:break;if (ct.HasChildren)setValue(ct.Controls, dt);/清空所有窗体输入控件的值public static void resetValue(Control.ControlColl

4、ection ctc)foreach (Control ct in ctc)switch (ct.GetType().Name)case “TextBox“:TextBox tb = (TextBox)ct;tb.Text = “;break;case “ComboBox“:ComboBox cb = (ComboBox)ct;cb.SelectedValue = 0;break;case “CheckBox“:CheckBox ck = (CheckBox)ct;ck.Checked = false;break;default:break;if (ct.HasChildren)resetVa

5、lue(ct.Controls);/获取窗体内容更新数据库的值串public static string getUpdateValues(Control.ControlCollection ctc)trystring values = “;values = getUpdateValuesLong(ctc, values);return replaceFirstMark(values);catch (Exception exp)MessageBox.Show(“错误:“ + exp.Message);return “;/获取窗体内容更新数据库的值串,前面有个逗号private static st

6、ring getUpdateValuesLong(Control.ControlCollection ctc, string values)foreach (Control ct in ctc)switch (ct.GetType().Name)case “TextBox“:TextBox tb = (TextBox)ct;values += “,“ + tb.Name + “=“ + tb.Text + “;break;case “ComboBox“:ComboBox cb = (ComboBox)ct;if (cb.SelectedValue = null)values += “,“ +

7、cb.Name + “=null“;elsevalues += “,“ + cb.Name + “=“ + cb.SelectedValue.ToString();break;case “CheckBox“:CheckBox ck = (CheckBox)ct;values += “,“ + ck.Name + “=“ + ck.Checked.ToString();break;default:break;if (ct.HasChildren)values = getUpdateValuesLong(ct.Controls, values);return values;/获取窗体内容追加数据库

8、的值串public static string getInsertValues(Control.ControlCollection ctc)trystring values = “;values = getInsertValuesLong(ctc, values);return replaceFirstMark(values);catch (Exception exp)MessageBox.Show(“错误:“ + exp.Message);return “;/获取窗体内容追加数据库的值串,前面有个逗号private static string getInsertValuesLong(Cont

9、rol.ControlCollection ctc, string values)foreach (Control ct in ctc)switch (ct.GetType().Name)case “TextBox“:TextBox tb = (TextBox)ct;values += “,“ + tb.Text + “;break;case “ComboBox“:ComboBox cb = (ComboBox)ct;if (cb.SelectedValue = null)values += “,null“;elsevalues += “,“ + cb.SelectedValue.ToStri

10、ng();break;case “CheckBox“:CheckBox ck = (CheckBox)ct;values += “,“ + ck.Checked.ToString();break;default:break;if (ct.HasChildren)values = getInsertValuesLong(ct.Controls, values);return values;/获取窗体内容追加数据库的字段名串public static string getInsertFields(Control.ControlCollection ctc)trystring fields = “;

11、fields = getInsertFieldsLong(ctc, fields);return replaceFirstMark(fields);catch (Exception exp)MessageBox.Show(“错误:“+exp.Message);return “;/获得追加数据库的字段名串,前面有个逗号private static string getInsertFieldsLong(Control.ControlCollection ctc, string fields)foreach (Control ct in ctc)switch (ct.GetType().Name)c

12、ase “TextBox“:TextBox tb = (TextBox)ct;fields += “,“ + tb.Name;break;case “ComboBox“:ComboBox cb = (ComboBox)ct; fields += “,“ + cb.Name; break;case “CheckBox“:CheckBox ck = (CheckBox)ct;fields += “,“ + ck.Name;break;default:break;if (ct.HasChildren)fields = getInsertFieldsLong(ct.Controls, fields);return fields;/去掉字符创的前导逗号private static string replaceFirstMark(string str)if (!str.Equals(String.Empty)str = str.Substring(1, str.Length - 1);return str; 窗体中 ComboBox 控件采用显示,值的方法加入。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号