none
请教一个winform当中窗体的show()问题 RRS feed

  • 问题

  • 在开发当中遇到一个奇怪的问题,比如如下代码

    Form1 frm1 = new Form1();

    frm1.Show();//重要的是这里,

    我的Form1当中有两个datagridview控件,一个显示所有商品,一个显示某个商品的适用等级(注意:这个datagridview是有一个checkbox列的)

    比如我在Form1当中的构造函数写了一些初始化的方法:Init(),加载商品,然后默认选择第一条商品把它的适用等级加载出来,这个时候如果已存在数据库当中的,就选中相应的checkbox,跟踪的时候是有去执行的,但是Form1 frm1=new Form1()执行完,执行Show()显示窗体后,又没有默认勾选数据库当中有的数据,要再点击一次商品datagridview控件才可以默认选中

    如果我在frm1.Show()然再调用一次init()就恢复正常了,

    这个问题很奇怪啊,难道Show会把之前选中的checkbox去掉,

    不知道我有没有描述清楚我的问题,谢谢各位啦

     


    crazyBasketball
    2010年11月18日 7:33

答案

  • 你好!

    建议使用 BindingSource 绑定数据源,通过修改 checkbox 列对应数据源的内容达到默认选中的。


    知识改变命运,奋斗成就人生!
    2011年3月16日 16:16
    版主

全部回复

  • 在开发当中遇到一个奇怪的问题,比如如下代码

    Form1 frm1 = new Form1();

    frm1.Show();//重要的是这里,

    我的Form1当中有两个datagridview控件,一个显示所有商品,一个显示某个商品的适用等级(注意:这个datagridview是有一个checkbox列的)

    比如我在Form1当中的构造函数写了一些初始化的方法:Init(),加载商品,然后默认选择第一条商品把它的适用等级加载出来,这个时候如果已存在数据库当中的,就选中相应的checkbox,跟踪的时候是有去执行的,但是Form1 frm1=new Form1()执行完,执行Show()显示窗体后,又没有默认勾选数据库当中有的数据,要再点击一次商品datagridview控件才可以默认选中

    如果我在frm1.Show()然再调用一次init()就恢复正常了,

    这个问题很奇怪啊,难道Show会把之前选中的checkbox去掉,

    不知道我有没有描述清楚我的问题,谢谢各位啦

     


    crazyBasketball

    dear
    并非Show把资料清掉,而是Form1 frm1 = new Form1()把资料清掉,
    1.你应该在Form1 frm1 = new Form1(),后再执行Init(),然后在Show出来。
    2.执行Show的时后会触发Form_Load事件,所以你也可以把Init()摆在Form_Load事件里。

     


    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
    2010年11月18日 7:58
  • 我最早也是这么写的,把init放到form_load当中去,就是不会默认选中checkbox

    我刚刚修改成放到form_load当中去了,也还是不行,要再点击一下商品datagridview控件,执行一次datagridview的CellClick的事件才可以默认勾选已经设置的等级


    crazyBasketball
    2010年11月18日 8:35
  • 不知道你是怎么实现的

    如果是我的话基本上会使用和DataGridView绑定的DataTable, 

    CheckBox列自然也和DataTable中的某一列绑定,这一列中只存储"0"或者"1",设置为对应CheckBox的选择或者未选择状态

    只要DataTable中的值设对了并且AcceptChanges()了,CheckBox不可能出现你所说的不一致的情况。

     

    我一般不会把这些处理写在构造函数里,还是用Form_Load比较安心。

    2010年11月18日 8:48
  • 应该不至于会如此,可否贴出部份程式码
    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
    2010年11月18日 8:50
  • 我也是写在Form_Load当中的
    crazyBasketball
    2010年11月18日 9:12
  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using HWS.BLL;
    using HWS.ENTITY;
    using HWS.TOOLS;
    
    namespace HWS.WIN
    {
      #region FrmGlobalGoodsList
      /// <summary>
      /// 商品列表!
      /// </summary>
      public partial class FrmGlobalGoodsList : TemplateMdiChildForm
      {
        #region field
    
        #region OperateType
        /// <summary>
        /// 操作类型,默认为更新!
        /// </summary>
        private EOperation type = EOperation.Update;
        #endregion
    
        #endregion
    
        #region Constractor
        /// <summary>
        /// 构造函数,如果用show的话,请再调用一次Init()方法
        /// </summary>
        public FrmGlobalGoodsList()
        {
          InitializeComponent();      
          this.InitEvent();
          //this.Init();
        }
        #endregion
    
        #region Properties
    
    
        //#region BllObj
        //private BllGoodsOwner m_BllObj;
        ///// <summary>
        ///// 只读属性,获取Bll层对象!
        ///// </summary>
        //private BllGoodsOwner BllObj
        //{
        //  get
        //  {
        //    //if (m_BllObj == null)
        //    {
        //      m_BllObj = new BllGoodsOwner();
        //    }
        //    return m_BllObj;
        //  }
        //}
        //#endregion
    
        #region BllObjGoods
        private BllGoods m_BllObjGoods;
        /// <summary>
        /// 只读属性,获取Bll层对象!
        /// </summary>
        private BllGoods BllObjGoods
        {
          get
          {
            if (m_BllObjGoods == null)
            {
              m_BllObjGoods = new BllGoods();
            }
            return m_BllObjGoods;
          }
          set 
          {
            m_BllObjGoods = value;
          }
        }
        #endregion
    
    
    
        #region CurrentRowGoods
        private BT_GOODS m_CurrentRowGoods = null;
        /// <summary>
        /// 可读写属性,获取当前选中行用户对象!
        /// </summary>
        private BT_GOODS CurrentRowGoods
        {
          get
          {
            if (this.dgvList.CurrentRow != null && this.dgvList.Rows.Count > 0 && this.DataSourceGoods.Count > 0)
            {
              m_CurrentRowGoods = BllObjGoods.SelectByID(((VIEW_GLOBAL_GOODS)this.dgvList.CurrentRow.DataBoundItem).GDS_ID);
            }
            else 
            {
              m_CurrentRowGoods = null;
            }
            return m_CurrentRowGoods;
          }
          set
          {
            m_CurrentRowGoods = value;
          }
        }
        #endregion
    
        #region DataSource
        private BindingSource m_DataSource;
        /// <summary>
        /// 只读属性,获取当前窗体数据源!
        /// </summary>
        private BindingSource DataSource
        {
          get
          {
            if (m_DataSource == null)
            {
              m_DataSource = new BindingSource();
            }
            return m_DataSource;
          }
        }
        #endregion
    
        #region DataSourceGoods
        private BindingSource m_DataSourceGoods;
        /// <summary>
        /// 只读属性,获取当前窗体数据源!
        /// </summary>
        private BindingSource DataSourceGoods
        {
          get
          {
            if (m_DataSourceGoods == null)
            {
              m_DataSourceGoods = new BindingSource();
            }
            return m_DataSourceGoods;
          }
        }
        #endregion
    
        //#region FrmCopy
        //private FrmGoodsCopy m_FrmCopy;
        ///// <summary>
        ///// 只读属性,获取商品复制窗体对象!
        ///// </summary>
        //private FrmGoodsCopy FrmCopy 
        //{
        //  get 
        //  {
        //    if (m_FrmCopy == null) 
        //    {
        //      m_FrmCopy = new FrmGoodsCopy();
        //    }
        //    return m_FrmCopy;
        //  }
        //}
        //#endregion
    
        #endregion
    
        #region Method
    
        #region InitEvent
        /// <summary>
        /// 初始化事件!
        /// </summary>
        private void InitEvent()
        {
          this.Load += new EventHandler(FrmGoodsList_Load);
          this.btnNew.Click += new EventHandler(btnNew_Click);
          this.btnEdit.Click += new EventHandler(btnEdit_Click);
          this.btnDelete.Click += new EventHandler(btnDelete_Click);
          this.btnSave.Click += new EventHandler(btnSave_Click);
          this.btnClose.Click += new EventHandler(btnClose_Click);
          this.btnCancel.Click += new EventHandler(btnCancel_Click);
          this.dgvList.CellClick += new DataGridViewCellEventHandler(dgvList_CellClick);
          //this.gvOwnerList.CellClick += new DataGridViewCellEventHandler(gvOwnerList_CellClick);
          //this.btnGoodsCopy.Click += new EventHandler(btnGoodsCopy_Click);
          this.tabControl1.Selected += new System.Windows.Forms.TabControlEventHandler(this.tabControl1_Selected);
          //this.dgvOwnerList.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvOwnerList_CellValueChanged);
          this.dgvOwnerList.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvOwnerList_CellValueChanged);
          dgvGoodsLevel.CellClick += new DataGridViewCellEventHandler(dgvGoodsLevel_CellClick);
          dgvGoodsLevel.CellValueChanged += new DataGridViewCellEventHandler(dgvGoodsLevel_CellValueChanged);
    
        }
    
        #endregion
    
        #region Init
        /// <summary>
        /// 初始化窗体,如果用show的话,请再调用一次Init()方法
        /// </summary>
        public void Init()
        {
          MustInputControls = new Control[] { label5, label7, label9, label6, label4, label2, label13 };
          CStyleManage.CurrentStyle.SetDataGridViewStyle(dgvList);
          CStyleManage.CurrentStyle.SetDataGridViewStyle(dgvOwnerList);
          CStyleManage.CurrentStyle.SetDataGridViewStyle(dgvGoodsLevel);
          BllCommonCode bll = new BllCommonCode();
          List<BT_COMMON_CODE> list = bll.SelectAllByType(enumCommonCodeType.Units);
          CCommon.BindComobBox(this.cmbUnit, "CMCD_NAME", "CMCD_CODE", list);
          //货类
          BllCommonCode bllCommonCode = new BllCommonCode();
          CCommon.BindComobBox(this.cbxGoodsType, "CMCD_NAME", "CMCD_CODE", bllCommonCode.SelectAllByType(enumCommonCodeType.GoodsType));
          RefreshMustInputControl();
          this.SearchData(string.Empty);
        }
        #endregion
    
        #region SearchData
        /// <summary>
        /// 打开窗体时,自动执行查询条件,默认查夜条件为空!
        /// </summary>
        /// <param name="keyWord">查询条件</param>
        private void SearchData(string keyWord)
        {
          try
          {
            //this.DataSourceGoods.DataSource = this.BllObjGoods.SelectView();
            BllObjGoods = null;
            type = EOperation.Update;
            DataSourceGoods.DataSource = BllObjGoods.SelectGlobalGoods();
            this.dgvList.DataSource = this.DataSourceGoods;
            if (DataSourceGoods.Count > 0)
            {
              dgvList.CurrentCell = dgvList.Rows[0].Cells[0];
              this.dgvList_CellClick(this.dgvList, new DataGridViewCellEventArgs(0, 0));
              tabControl1.SelectedTab = tabGoodsOwner;
              tabControl1.SelectedTab = tabGoodsLevel;
              tabControl1.SelectedTab = tabGoodsOwner;
            }
            //List<BT_GOODS_OWNER> list = this.BllObj.SelectAll().ToList();
            //if (this.paraCurrentOwnerID != Guid.Empty)
            //{
            //  list = list.Where(p => p.GWN_ID == paraCurrentOwnerID).ToList();
            //}
    
            //this.DataSource.DataSource = list;
    
            //this.gvOwnerList.DataSource = this.DataSource;
            //if(this.DataSource.Count > 0)
            //{
            //  this.gvOwnerList_CellClick(this.gvOwnerList, new DataGridViewCellEventArgs(0, 0));
            //}
            //this.SetBtnEnabled(true);
    
            //以下语句是因为发现第一次触发时不显示勾,第二次以后就正常了,没有解决方案,所以才不得已这么做
            //this.tabControl1.SelectedTab = this.tabPageBaseInfo;
            //this.tabControl1.SelectedTab = this.tabGoodsOwner;
            //this.tabControl1.SelectedTab = this.tabPageBaseInfo;
    
    
            this.SetBtnEnabled(true);
          }
          catch (Exception ex)
          {
            CMsg.ShowMsg(ex.Message);
          }
        }
        #endregion
    
        #region DisplayData
        /// <summary>
        /// 显示数据!
        /// </summary>
        /// <param name="obj">用户对象,新增时传null就可以了</param>
        public void DisplayData(BT_GOODS obj)
        {
          Cursor.Current = Cursors.WaitCursor;
          if (obj == null)
          {
            obj = this.InitObject();
            //obj.GDS_GWN_ID = CurrentRow.GWN_ID;
          }
          this.txtRemark.Text = obj.GDS_REMARK;
          this.txtPrice.Text = obj.GDS_PRICE.HasValue ? obj.GDS_PRICE.ToString() : "";
          this.txtThroughPutWeight.Text = obj.GDS_THROUGHPUT_WEIGHT.HasValue ? obj.GDS_THROUGHPUT_WEIGHT.ToString() : "";
          this.txtName.Text = obj.GDS_NAME;
          this.txtModel.Text = obj.GDS_MODEL;
          this.txtDetailNum.Text = obj.GDS_DETAILS_NUM.HasValue ? obj.GDS_DETAILS_NUM.ToString() : "";
          this.txtCode.Text = obj.GDS_CODE;
          if (obj.GDS_UNITS_CODE != null)
          {
            this.cmbUnit.SelectedValue = obj.GDS_UNITS_CODE;
          }
          if (obj.GDS_GOODTYPE_CODE != null)
          {
            this.cbxGoodsType.SelectedValue = obj.GDS_GOODTYPE_CODE;
          }
          LoadOwnerList(obj.GDS_ID);
          LoadGoodsLevelList(obj.GDS_ID);
          Cursor.Current = Cursors.WaitCursor;
        }
        #endregion
    
        #region SetData
        /// <summary>
        /// 设置数据
        /// </summary>
        /// <param name="obj">用户对象</param>
        private void SetData(BT_GOODS obj)
        {
          obj.GDS_CODE = txtCode.Text.Trim();
          if (this.txtDetailNum.Text.Trim().Length == 0)
          {
            obj.GDS_DETAILS_NUM = null;
          }
          else
          {
            obj.GDS_DETAILS_NUM = Convert.ToDecimal(txtDetailNum.Text.Trim());
          }
          obj.GDS_MODEL = txtModel.Text.Trim();
          //obj.GDS_GWN_ID = this.CurrentOwnerID;
          obj.GDS_NAME = this.txtName.Text.Trim();
          if (this.txtPrice.Text.Trim().Length != 0)
          {
            obj.GDS_PRICE = Convert.ToDecimal(this.txtPrice.Text.Trim());
          }
          else
          {
            obj.GDS_PRICE = null;
          }
    
          obj.GDS_REMARK = this.txtRemark.Text.Trim();
          obj.GDS_UNITS_CODE = (string)this.cmbUnit.SelectedValue;
          obj.GDS_THROUGHPUT_WEIGHT = this.txtThroughPutWeight.Value;
          obj.GDS_GOODTYPE_CODE = this.cbxGoodsType.SelectedValue.ToString();
        }
        #endregion
    
        #region InitObject
        /// <summary>
        /// 初始化对象
        /// </summary>
        /// <returns></returns>
        private BT_GOODS InitObject()
        {
          BT_GOODS obj = new BT_GOODS();
          obj.GDS_ID = Guid.NewGuid();
          obj.GDS_DETAILS_NUM = 1;
          obj.GDS_THROUGHPUT_WEIGHT = 0;
          return obj;
        }
        #endregion
    
        #region BeforeCheck
        /// <summary>
        /// 保存前进行数据检查!
        /// </summary>
        /// <returns></returns>
        private bool BeforeCheck()
        {
          if (this.txtName.Text.Trim().Length == 0 || this.txtName.Text.Trim().Length > 30)
          {
            CMsg.ShowMsg("请输入名称名称!");
            this.txtName.Focus();
            return false;
          }
          if (string.IsNullOrEmpty(txtModel.Text.Trim())) 
          {
            CMsg.ShowMsg("请输入商品规格!");
            txtModel.Focus();
            return false;
          }
          if (cbxGoodsType.Items.Count == 0 || cbxGoodsType.SelectedIndex == -1) 
          {
            CMsg.ShowMsg("请选择货类!");
            return false;
          }
          if (cmbUnit.Items.Count == 0 || cmbUnit.SelectedIndex == -1)
          {
            CMsg.ShowMsg("请选择单位!");
            return false;
          }
          if (string.IsNullOrEmpty(txtDetailNum.Text)) 
          {
            CMsg.ShowMsg("请输入细数");
            return false;
          }
          if (string.IsNullOrEmpty(txtThroughPutWeight.Text)) 
          {
            CMsg.ShowMsg("请选择吞吐量!");
            return false;
          }
          if (this.txtCode.Text.Trim().Length == 0 && new BllRule().GetRule("codeAutoGenerate") == 0)
          {
            CMsg.ShowMsg("代码不能为空!");
            this.txtCode.Focus();
            return false;
          }
          var count = SelectRowsCount(colGoodsLeveSelected.Name, dgvGoodsLevel);
          if (count == 0)
          {
            CMsg.ShowMsg("请至少选择一种货物适用等级!");
            return false;
          }
          return true;
        }
        #endregion
    
        #region SetBtnEnabled
        /// <summary>
        /// 设置按钮的可用性!
        /// </summary>
        private void SetBtnEnabled(bool flag)
        {
          this.btnNew.Enabled = this.btnEdit.Enabled = this.btnDelete.Enabled = this.dgvList.Enabled = flag;
          this.btnSave.Enabled = this.btnCancel.Enabled = !flag;
          CCommon.SetControlStatus(groupBox3, !flag);
    
          //CCommon.SetControlStatus( tabPageControlInfo, !flag);
          if (!flag) this.txtCode.Enabled = new BllRule().GetRule("codeAutoGenerate") == 0 ? true : false;
        }
        #endregion
    
        #region 全选或者反选dataGridView选择列
        /// <summary>
        /// 全选或者反选dataGridView选择列
        /// </summary>
        /// <param name="colName">选择列的列名</param>
        /// <param name="dgv">dataGridView对象</param>
        /// <param name="IsSelected">true表示选中,false选择不选中</param>
        private void SelectGridViewRows(string colName, DataGridView dgv, bool IsSelected) 
        {
          if (dgv == null || dgv.RowCount == 0) return;
          if (!dgv.Columns.Contains(colName)) return;
          foreach (DataGridViewRow row in dgv.Rows) 
          {
            row.Cells[colName].Value = IsSelected;
          }
        }
        #endregion
    
        #region 选中的行数
        /// <summary>
        /// 选中的行数!
        /// </summary>
        /// <param name="colName">选择列的列名</param>
        /// <param name="dgv">dataGridView对象</param>
        /// <returns>返回被选择的行数,为空返回0</returns>
        private int SelectRowsCount(string colName, DataGridView dgv)
        {
          var count = 0;
          if (dgv == null || dgv.RowCount == 0) return count;
          if (!dgv.Columns.Contains(colName)) return count;
          foreach (DataGridViewRow row in dgv.Rows)
          {
            var selected = row.Cells[colName].Value;
            if (selected != null && Convert.ToBoolean(selected)) 
            {
              count++;
            }
          }
          return count;
        }
        #endregion
    
        #region 加载所有适用货主
        /// <summary>
        /// 加载所有适用货主
        /// </summary>
        /// <param name="goodsId">商品ID</param>
        private void LoadOwnerList(Guid goodsId) 
        {
          handlerOwnerCellValueChanged = false;
          var selectColumnName = colSelected.Name;
          List<BT_GOODS_OWNER> list = new BllGoodsOwner().SelectAll().ToList();//TODO:这边以后可能要用字典把所有的货主先存起来,避免重复查询
          this.dgvOwnerList.DataSource = list;
          if (CurrentRowGoods == null || type == EOperation.Insert) //当前行为空,说明是新增,直接返回
          {
            //清空选中的记录
            SelectGridViewRows(selectColumnName, dgvOwnerList, false);
            handlerOwnerCellValueChanged = true;
            return;
          }
          List<BT_GOODS_OF_OWNER> goodsOwners = new BllGoodsOfOwner().SelectByGoodsID(this.CurrentRowGoods.GDS_ID);      
          foreach (DataGridViewRow row in this.dgvOwnerList.Rows) 
          {
            var ownerId = ((BT_GOODS_OWNER)row.DataBoundItem).GWN_ID;
            var exists = goodsOwners.Where(id => id.GDS_GWN_ID == ownerId).FirstOrDefault();
            if (exists == null) continue;
            row.Cells[selectColumnName].Value = true;
          }
          handlerOwnerCellValueChanged = true;
        }
        #endregion
    
        #region 加载所有的适用等级
        /// <summary>
        /// 加载所有的适用等级
        /// </summary>
        /// <param name="goodsId">商品ID</param>
        private void LoadGoodsLevelList(Guid goodsId)
        {
          handlerGoodsLevelCellValueChanged = false;
          var selectColumnName = colGoodsLeveSelected.Name;
          List<BT_COMMON_CODE> list = new BllCommonCode().SelectAllByType(enumCommonCodeType.GoodsLevel);//TODO:这边以后可能要用字典把所有的等级先存起来,避免重复查询
          this.dgvGoodsLevel.DataSource = list;
          if (CurrentRowGoods == null || type == EOperation.Insert) //当前行为空,说明是新增,直接返回
          {
            //清空选中的记录
            SelectGridViewRows(selectColumnName, dgvGoodsLevel, false);
            handlerGoodsLevelCellValueChanged = true;
            return;
          }
          List<BT_GOODS_OF_LEVEL> levelList = new BllGoodsOfLevel().SelectByGoodsID(CurrentRowGoods.GDS_ID);
          if (levelList == null || levelList.Count == 0)
          {
            handlerGoodsLevelCellValueChanged = true;
            return;
          }
          
          foreach (DataGridViewRow row in this.dgvGoodsLevel.Rows)
          {
            var levelCode = ((BT_COMMON_CODE)row.DataBoundItem).CMCD_CODE;
            var exists = levelList.Where(id => id.GDLV_LEVEL_CODE == levelCode).FirstOrDefault();
            if (exists == null) continue;
            row.Cells[selectColumnName].Value = true;
          }
          handlerGoodsLevelCellValueChanged = true;
        }
        #endregion
    
        #endregion
    
        #region Event
    
        #region Load handler for Form
        void FrmGoodsList_Load(object sender, EventArgs e)
        {
          this.Init();
          //this.SearchData(string.Empty);
        }
        #endregion
    
        #region Click handler for btnClose
        /// <summary>
        /// 关闭按钮事件!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnClose_Click(object sender, EventArgs e)
        {
          Close();
        }
        #endregion
    
        #region Click handler for btnDelete
        /// <summary>
        /// 删除按钮事件!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnDelete_Click(object sender, EventArgs e)
        {
          if (this.CurrentRowGoods == null)
          {
            CMsg.ShowMsg("请选择您要删除的行!");
            return;
          }
          try
          {
            DialogResult dlg = CMsg.ShowMsg(CMsgCode.DeleteConfirm, this.CurrentRowGoods.GDS_NAME, MessageBoxButtons.YesNo);
            if (dlg == DialogResult.Yes)
            {
              new BllGoods().Delete(this.CurrentRowGoods.GDS_ID);
              CMsg.ShowMsg(CMsgCode.DeleteSuccessful);
              this.SearchData(string.Empty);
            }
            else if (dlg == DialogResult.No)
            {
              return;
            }
          }
          catch (Exception ex)
          {
            CMsg.ShowMsg(ex.Message);
          }
        }
        #endregion
    
        #region Click handler for btnEdit
        /// <summary>
        /// 编辑事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnEdit_Click(object sender, EventArgs e)
        {
          if (CurrentRowGoods == null)
          {
            CMsg.ShowMsg("请选择要修改的行!");
            return;
          }
          this.SetBtnEnabled(false);
          this.type = EOperation.Update;
        }
        #endregion
    
        #region Click handler for btnNew
        /// <summary>
        /// 新增同级组织按钮事件!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnNew_Click(object sender, EventArgs e)
        {
          this.type = EOperation.Insert;
          this.SetBtnEnabled(false);
          this.DisplayData(null);
          //设置适用货物的默认等级
          foreach (DataGridViewRow row in dgvGoodsLevel.Rows) 
          {
            var item = (BT_COMMON_CODE)row.DataBoundItem;
            if (item.CMCD_CODE == CCommon.CsDefaultGoodsLevelCode) 
            {
              row.Cells[colGoodsLeveSelected.Name].Value = true;
              break;
            }
          }
        }
        #endregion
    
        #region Click handler for btnSearch
        /// <summary>
        /// 查询按钮事件!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnSearch_Click(object sender, EventArgs e)
        {
          this.SearchData(string.Empty);
        }
        #endregion
    
        #region Click handler for btnSave
        /// <summary>
        /// 保存事件!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnSave_Click(object sender, EventArgs e)
        {
          bool flag = this.BeforeCheck();
          if (!flag) return;
          try
          {
            BT_GOODS obj;
            if (this.type == EOperation.Insert)
            {
              obj = new BT_GOODS();
              this.SetData(obj);
              new BllGoods().Insert(obj);
              //添加默认适用等级
              BT_GOODS_OF_LEVEL rec = new BT_GOODS_OF_LEVEL();
              rec.GDLV_LEVEL_CODE = CCommon.CsDefaultGoodsLevelCode;
              rec.GDLV_GOODS_ID = obj.GDS_ID;
              new BllGoodsOfLevel().Insert(rec);
              // this.outCurrentAddOwners.Add(obj); //保存到输出参数,以备其他窗口使用
              CMsg.ShowMsg(CMsgCode.SaveSuccessful);
    
            }
            else
            {
              obj = this.CurrentRowGoods;
              this.SetData(obj);
              new BllGoods().Update(obj);
              CMsg.ShowMsg(CMsgCode.SaveSuccessful);
            }        
            this.SearchData(string.Empty);
          }
          catch (Exception ex)
          {
            CMsg.ShowMsg(ex.Message);
          }
        }
        #endregion
    
        #region Click handler for btnCancel
        /// <summary>
        /// 取消事件!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btnCancel_Click(object sender, EventArgs e)
        {
          this.SearchData(string.Empty);
        }
        #endregion
    
        #region CellClick handler for dgvList
        /// <summary>
        /// 显示货物明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void dgvList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
          //显示基本信息
          if (this.CurrentRowGoods != null)
          {
            this.DisplayData(this.CurrentRowGoods);
          }
          else
          {
            CCommon.DisplayDefaultValue(groupBox3);
          }
        }
        #endregion
    
        #region Selected handler for tabControl1
        /// <summary>
        /// tab页切换事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
          //if (type == EOperation.Update) 
          //{
          //  CMsg.ShowMsg("请先保存商品信息!");
          //  return;
          //}
          #region original version discard by zhongsb 2010-11-16
    
          ////切换到定义货主界面时,货品必须已经保存
          //if (this.tabControl1.SelectedTab == this.tabGoodsOwner && this.btnSave.Enabled == true)
          //{
          //  CMsg.ShowMsg("请先保存商品信息!");
          //  return;
          //}
          ////控制初始话数据时,不会触发值变化的事件
          //this.dgvOwnerList.CellValueChanged -= new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvOwnerList_CellValueChanged);
          
          ////显示所有货主
          ////this.dgvOwnerList.DataSource = null;
          //List<BT_GOODS_OWNER> list = new BllGoodsOwner().SelectAll().ToList();
          //this.dgvOwnerList.DataSource = list;
          ////this.BindingContext[this.dgvOwnerList.DataSource].SuspendBinding();
          ////显示商品适用的货主
          //if (CurrentRowGoods == null) 
          //{
          //  this.dgvOwnerList.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvOwnerList_CellValueChanged);
          //  return;
          //}
          //List<BT_GOODS_OF_OWNER> goodsOwners = new BllGoodsOfOwner().SelectByGoodsID(this.CurrentRowGoods.GDS_ID);
          //var selectColumnName = colSelected.Name;
          //foreach (BT_GOODS_OF_OWNER rec in goodsOwners)
          //{
          //  foreach (DataGridViewRow row in this.dgvOwnerList.Rows)
          //  {
          //    var isSelect = rec.GDS_GWN_ID == ((BT_GOODS_OWNER)row.DataBoundItem).GWN_ID;
          //    row.Cells[selectColumnName].Value = isSelect;
          //  }
          //}
          ////this.BindingContext[this.dgvOwnerList.DataSource].ResumeBinding();
          //this.dgvOwnerList.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvOwnerList_CellValueChanged);
    
          #endregion
        }
        #endregion
    
        #region CellValueChanged handler for dgvOwnerList
        /// <summary>
        /// 是否处理货主单元格值变化事件
        /// </summary>
        private bool handlerOwnerCellValueChanged = false;
        /// <summary>
        /// 适用货主列表单元格值改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvOwnerList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
          if (e.RowIndex == -1 || e.ColumnIndex != 0) return;
          if (this.dgvOwnerList.CurrentRow == null) return;
          if (!handlerOwnerCellValueChanged) return;
          object val = this.dgvOwnerList[0, e.RowIndex].Value;
          var isSelect = Convert.ToBoolean(val);
          BT_GOODS_OF_OWNER rec = new BT_GOODS_OF_OWNER();
          rec.GDS_GWN_ID = ((BT_GOODS_OWNER)this.dgvOwnerList.CurrentRow.DataBoundItem).GWN_ID;
          rec.GDS_ID = this.CurrentRowGoods.GDS_ID;
          try
          {
            if (isSelect) //注意:无!
            {
              new BllGoodsOfOwner().Insert(rec);
            }
            else
            {
              new BllGoodsOfOwner().Delete(rec);
            }
          }
          catch (Exception ex)
          {
            handlerOwnerCellValueChanged = false;
            dgvOwnerList[0, e.RowIndex].Value = !isSelect;
            handlerOwnerCellValueChanged = true;
            CMsg.ShowMsg(ex.Message);
          }
        }
        #endregion
    
        #region CellClick handler for dgvOwnerList
        /// <summary>
        /// 适用货主列表单元格单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvOwnerList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
          if (e.RowIndex == -1 || e.ColumnIndex == -1 || e.ColumnIndex != colSelected.Index) return;
          if (this.dgvOwnerList.CurrentRow == null) return;
          if (type == EOperation.Insert)
          {
            CMsg.ShowMsg("请先保存货物!");
            return;
          }
          object val = this.dgvOwnerList[0, e.RowIndex].Value;
          if (val == null || !Convert.ToBoolean(val)) //注意!
          {
            this.dgvOwnerList[0, e.RowIndex].Value = true;
          }
          else
          {
            this.dgvOwnerList[0, e.RowIndex].Value = false;
          }
        }
        #endregion
    
        #region CellValueChanged handler for dgvGoodsLevel
        /// <summary>
        /// 是否处理适用等级单元格值变化事件
        /// </summary>
        private bool handlerGoodsLevelCellValueChanged = false;
        /// <summary>
        /// 适用等级列表单元格值改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void dgvGoodsLevel_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
          if (e.RowIndex == -1 || e.ColumnIndex == -1 || e.ColumnIndex != 0) return;
          if (this.dgvOwnerList.CurrentRow == null) return;
          if (!handlerGoodsLevelCellValueChanged) return;
          object val = this.dgvGoodsLevel[0, e.RowIndex].Value;
          var isSelect = Convert.ToBoolean(val);
          BT_GOODS_OF_LEVEL rec = new BT_GOODS_OF_LEVEL();
          rec.GDLV_LEVEL_CODE = ((BT_COMMON_CODE)this.dgvGoodsLevel.CurrentRow.DataBoundItem).CMCD_CODE;
          rec.GDLV_GOODS_ID = this.CurrentRowGoods.GDS_ID;
          try
          {
            if (isSelect) //注意:无!
            {
              new BllGoodsOfLevel().Insert(rec);
            }
            else
            {
              new BllGoodsOfLevel().Delete(rec);
            }
          }
          catch (Exception ex)
          {
            handlerGoodsLevelCellValueChanged = false;
            dgvGoodsLevel[0, e.RowIndex].Value = !isSelect;
            handlerGoodsLevelCellValueChanged = true;
            CMsg.ShowMsg(ex.Message);
          }
        }
        #endregion
    
        #region CellClick handler for dgvGoodsLevel
        /// <summary>
        /// 适用等级列表单元格单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void dgvGoodsLevel_CellClick(object sender, DataGridViewCellEventArgs e)
        {
          if (e.RowIndex == -1 || e.ColumnIndex == -1 || e.ColumnIndex != colGoodsLeveSelected.Index) return;
          if (this.dgvGoodsLevel.CurrentRow == null) return;
          if (type == EOperation.Insert)
          {
            CMsg.ShowMsg("请先保存货物!");
            return;
          }
          object val = this.dgvGoodsLevel[0, e.RowIndex].Value;
          if (val == null || !Convert.ToBoolean(val))
          {
            this.dgvGoodsLevel[0, e.RowIndex].Value = true;
          }
          else
          {
            this.dgvGoodsLevel[0, e.RowIndex].Value = false;
          }
        }
        #endregion
    
        #endregion
      }
      #endregion
    }
    
    可以,但是感觉代码有点乱,呵呵,等等,我整理一下
    crazyBasketball
    2010年11月18日 9:13
  • 你好!

    建议使用 BindingSource 绑定数据源,通过修改 checkbox 列对应数据源的内容达到默认选中的。


    知识改变命运,奋斗成就人生!
    2011年3月16日 16:16
    版主