• Winform中实现新增和更新共用一个页面的示例流程


    场景

    Winform中对某个表的数据查询出来,数据放在DataTable中,并将其显示在DataGrdiView中。

    点击新增和编辑时打开的是同一个页面。

    打开页面加载所有数据并显示

    点击新增

    点击编辑


     

    注:

    博客主页:
    https://blog.csdn.net/badao_liumang_qizhi
    关注公众号
    霸道的程序猿
    获取编程相关电子书、教程推送与免费下载。

    实现

    查询实现

    首先在Winform主页面的页面加载事件中,通过工具类中的查询方法查询数据并返回DataTable,然后通过id获取DataGridView,然后设置DataGridView每列的显示名称以及每列与DataTable所对应的属性名。然后绑定数据源。

               //查询所有数据
                DataTable table = KillSetHelper.SelectAllKillSet();
                //获取DataGridView
                DataGridView killLogGridView = this.GetControlById("KillSetGridView") as DataGridView;
                killLogGridView.Columns.Clear();
    
                DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn();
                acCode.DataPropertyName = "HomeNo";
                acCode.HeaderText = "房间号";
                killLogGridView.Columns.Add(acCode);
    
                DataGridViewTextBoxColumn acCode2 = new DataGridViewTextBoxColumn();
                acCode2.DataPropertyName = "KillWay";
                acCode2.HeaderText = "消杀方式";
                killLogGridView.Columns.Add(acCode2);
    
                DataGridViewTextBoxColumn acCode3 = new DataGridViewTextBoxColumn();
                acCode3.DataPropertyName = "Components";
                acCode3.HeaderText = "消杀部件";
                acCode3.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                killLogGridView.Columns.Add(acCode3);
    
    
                DataGridViewTextBoxColumn acCode4 = new DataGridViewTextBoxColumn();
                acCode4.DataPropertyName = "KillSeconds";
                acCode4.HeaderText = "定点消杀秒数";
                killLogGridView.Columns.Add(acCode4);
    
                killLogGridView.DataSource = table;
                killLogGridView.AutoGenerateColumns = false;

    新增实现

    新建一个新增页面,修改其布局,添加与数据库字段对应的要添加的属性。

    然后打开此新增页面的代码,

    首先定义一个private的枚举类型字段

    private ActionType _actionType = ActionType.Add;    //操作类别,默认为添加

    此枚举类型定义为

        public enum ActionType
        {
            /// <summary>
            /// 添加
            /// </summary>
            Add = 0,
            /// <summary>
            /// 修改
            /// </summary>
            Modify = 1
        }

    然后再新增一个带参数的构造方法,参数为上面的ActionType,在构造方法中将参数赋值给上面的字段。

    还要保留原有的不带参数的构造方法

      

          public FrmAddKillSet()
            {
                InitializeComponent();
            }
    
            public FrmAddKillSet(ActionType actionType)
            {
                InitializeComponent();
                this._actionType = actionType;
            }

    这样在新建新增页面时就能指定当前操作类型是新增还是更新

    FrmAddKillSet frmInsert = new FrmAddKillSet(ActionType.Add);

    还要定义一个public的类型为一个自定义Model的属性用来传递要新增和更新的一些属性。

    比如这里要添加的和更新的属性有如下

       public class KillSetModel
        {
            /// <summary>
            /// 房间号
            /// </summary>
            private string homeNo;
            /// <summary>
            /// 消杀方式
            /// </summary>
            private int killWay;
            /// <summary>
            /// 定点消杀秒数
            /// </summary>
            private int killSeconds;
            /// <summary>
            /// 消杀部件
            /// </summary>
            private List<int> components;
            /// <summary>
            /// 房间号
            /// </summary>
            public string HomeNo
            {
                get { return homeNo; }
                set { homeNo = value; }
            }
            /// <summary>
            /// 消杀方式
            /// </summary>
            public int KillWay
            {
                get { return killWay; }
                set { killWay = value; }
            }
            /// <summary>
            /// 定点消杀秒数
            /// </summary>
            public int KillSeconds
            {
                get { return killSeconds; }
                set { killSeconds = value; }
            }
            /// <summary>
            /// 消杀部件
            /// </summary>
            public List<int> Components
            {
                get { return components; }
                set { components = value; }
            }
        }

    所以这里定义的属性为

           public KillSetModel KillSetModel
            {
                get
                {
                 }
                set
                {
                 }
            }

    然后在KillSetModel中的get方法中获取页面上各个控件的值并将其赋值给KillSetModel的各个属性

               get
                {
                    KillSetModel killSetModel = new KillSetModel();
                    killSetModel.HomeNo = this.homeNo.Text;
                    if(this.moveKill.Checked)
                    {
                        killSetModel.KillWay = Global.KILL_WAY_MOVE_KILL;
                    }
                    if (this.fixKill.Checked)
                    {
                        killSetModel.KillWay = Global.KILL_WAY_FIX_KILL;
                        int killSeconds;
                        int.TryParse(this.killSeconds.Text,out killSeconds);
                        killSetModel.KillSeconds = killSeconds;
                    }
                    List<int> components = new List<int>();
                    if(this.CB_1.Checked)
                    {
                        components.Add((int)KillComponents.ShangWuHua);                
                    }
                    if (this.CB_2.Checked)
                    {
                        components.Add((int)KillComponents.XiaWuHua);
                    }
                    if (this.CB_3.Checked)
                    {
                        components.Add((int)KillComponents.ShangZiWai);
                    }
                    if (this.CB_4.Checked)
                    {
                        components.Add((int)KillComponents.XiaZiWai);
                    }
                    if (this.CB_5.Checked)
                    {
                        components.Add((int)KillComponents.ShengJiangZhu);
                    }
                    if (this.CB_6.Checked)
                    {
                        components.Add((int)KillComponents.KongQiJingHua);
                    }
                    killSetModel.Components = components;
                    return killSetModel;
                }

    这样就可以在新增页面返回OK后获取到新增时的值

    FrmAddKillSet frmInsert = new FrmAddKillSet(ActionType.Add);
    string homeNo = frmInsert.KillSetModel.HomeNo;

    然后在新增按钮的点击事件中,直接返回DialogResult为OK就行了

    this.DialogResult = System.Windows.Forms.DialogResult.OK;

    在上面新增按钮的点击事件中

      

              //传递当前操作类型为新增
                FrmAddKillSet frmInsert = new FrmAddKillSet(ActionType.Add);
                //窗口显示在屏幕中间
                frmInsert.StartPosition = FormStartPosition.CenterScreen;
                //显示窗口
                DialogResult result = frmInsert.ShowDialog();
                //如果窗口返回OK
                if (result == DialogResult.OK)
                {
                    string strSql = "";
                    string homeNo = frmInsert.KillSetModel.HomeNo;
                    int killWay = frmInsert.KillSetModel.KillWay;
                    int killSeconds = frmInsert.KillSetModel.KillSeconds;
                    List<int> components = frmInsert.KillSetModel.Components;
                    //执行插入数据库的操作
                 }

    编辑实现

    在页面编辑按钮的点击事件中,在上面已经对新增实现的基础上

    要根据选中行的第一个属性查询出所有的属性然后传递到新增页面的KillSetModel中,

    在此KillSetModel的set方法中对新增页面的各个控件进行赋值,然后再点击更新按钮后将

    返回页面结果为OK此时再通过KillSetModel属性的get方法对应的操作获取控件的值并传递到更新按钮的点击

    事件中,然后获取各个属性并执行更新数据的操作。

    首先在共用的新增页面的属性KillSetModel的set方法中

               set
                {
                    this._killSetModel = value;
                    //房间号赋值
                    this.homeNo.Text = this._killSetModel.HomeNo;
                    this.homeNo.Enabled = false;
                    this.BT_ADD.Text = "更新";
                    
                    //
                    if (this._killSetModel.KillWay == Global.KILL_WAY_MOVE_KILL)
                    {
                        this.moveKill.Checked = true;
                    }
                    if (this._killSetModel.KillWay == Global.KILL_WAY_FIX_KILL)
                    {
                        this.fixKill.Checked = true;
                        this.killSeconds.Text = this._killSetModel.KillSeconds.ToString();
                    }
                    //
                    foreach (int index in this._killSetModel.Components)
                    {
                        switch (index)
                            {
                            case (int)KillComponents.ShangWuHua :
                                    this.CB_1.Checked = true;
                                    break;
                            case (int)KillComponents.XiaWuHua:
                                    this.CB_2.Checked = true;
                                    break;
                            case (int)KillComponents.ShangZiWai:
                                    this.CB_3.Checked = true;
                                    break;
                            case (int)KillComponents.XiaZiWai:
                                    this.CB_4.Checked = true;
                                    break;
                            case (int)KillComponents.ShengJiangZhu:
                                    this.CB_5.Checked = true;
                                    break;
                            case (int)KillComponents.KongQiJingHua:
                                    this.CB_6.Checked = true;
                                    break;
                            }
                    }
                }

    然后在更改后点击更新后直接返回DialogResult为OK

    this.DialogResult = System.Windows.Forms.DialogResult.OK;

    然后在前面更新按钮的点击事件中

       

             DataGridView killSetGridView = this.GetControlById("KillSetGridView") as DataGridView;
    
                if (killSetGridView.SelectedRows.Count == 0)
                {
                    MessageBox.Show("请选中一条数据!");
                    return;
                }
    
                DataGridViewRow row = killSetGridView.SelectedRows[0];
                string homeNo = row.Cells[0].Value.ToString();
                KillSetModel model = new KillSetModel();
                if (!string.IsNullOrEmpty(homeNo))
                {
      //查询出对应的Model
                    DataTable table = KillSetHelper.SelectKillSetByHomeNo(homeNo);
                    
                    model.HomeNo = table.Rows[0][0].ToString();
                    model.KillWay = int.Parse(table.Rows[0][1].ToString());
                    if (model.KillWay == Global.KILL_WAY_FIX_KILL)
                    {
                        model.KillSeconds = int.Parse(table.Rows[0][3].ToString());
                    }
    
                    string[] arrayComponents = table.Rows[0][2].ToString().Split(',');
                    List<int> listComponents = new List<int>();
                    foreach(string a in arrayComponents)
                    {
                        listComponents.Add(int.Parse(a));
                    }
                    model.Components = listComponents;
                }
    
                //更新
                FrmAddKillSet frmInsert = new FrmAddKillSet(ActionType.Modify);
                //传递参数
                frmInsert.KillSetModel = model;
                frmInsert.StartPosition = FormStartPosition.CenterScreen;
                DialogResult result = frmInsert.ShowDialog();           
                if (result == DialogResult.OK)
                {
                    string strSql = "";        
                    int killWay = frmInsert.KillSetModel.KillWay;
                    int killSeconds = frmInsert.KillSetModel.KillSeconds;
                    List<int> components = frmInsert.KillSetModel.Components;
                    string componentsNew = "";
                    foreach (int i in components)
                    {
                        componentsNew += (i + ",");
                    }
                    componentsNew = componentsNew.Substring(0, componentsNew.Length - 1);  
      //下面执行更新数据的操作
          }
  • 相关阅读:
    Napa简介——基于Web用于Outlook/SharePoint的IDE
    Azul开源Zing Jvm
    如何控制单元测试的粒度?
    存储过程与Entity Framework
    Java增强的for循环和普通for循环对比
    Java中的static变量、方法、代码块初始化执行顺序
    推荐一款好用的博客软件——菊子曰
    Android进程与线程
    OPhone SDK
    开始学习Android
  • 原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/12761462.html
Copyright © 2020-2023  润新知