数据控制方面的类

本人第一次在这里发表文章,我写的一个数据控制方面的类,在C/S下自己用的比较顺手,请大家给些建议

 public class DataControl
 {
  private CurrencyManager m_CM;
  public DataControl()
  {
  
 
  }

  /// <summary>
  /// 构建
  /// </summary>
  /// <param name="ds"></param>
  /// <param name="TableName"></param>
  /// <param name="SelfForm"></param>
  public DataControl(DataSet ds,string TableName,Form SelfForm)
  {
   this.m_CM=SelfForm.BindingContext[ds,TableName] as CurrencyManager ;
  }

  private DevExpress.XtraBars.BarStaticItem m_EditCount;

  public DevExpress.XtraBars.BarStaticItem EditCount
  {
   set
   {
    m_EditCount=value;
   }
  }

  private TextBox m_EditCurrentNum;

  public TextBox EditCurrentNum
  {
   set
   {
    m_EditCurrentNum=value;
   }
  }

  private System.Windows.Forms.StatusBarPanel m_DataPanel;
  public System.Windows.Forms.StatusBarPanel DataPanel
  {
   set
   {
    m_DataPanel=value;
   }
  }


  /// <summary>
  /// 数据管理者
  /// </summary>
  public CurrencyManager CM
  {
   set
   {
    m_CM=value;
    if (m_CM!=null)
    {
     this.CM.PositionChanged-=new EventHandler(CM_PositionChanged);
     this.CM.PositionChanged+=new EventHandler(CM_PositionChanged);
     if (m_EditCount!=null)
     {
      m_EditCount.Caption=this.m_CM.Count.ToString();
      int intCur=this.m_CM.Position+1;
      m_EditCurrentNum.Caption=intCur.ToString();
     }
    }
    else
    {
     m_CM=null;
     if (m_EditCount!=null)
     {
      m_EditCount.Caption="0";
      m_EditCurrentNum.Caption="0";
     }
    }
   }
   get{return m_CM;}
  }


  /// <summary>
  /// 添加一条空记录
  /// </summary>
  public void Add()
  {
   if (m_CM!=null)
   {
    if (beforeInsert!=null)
    {
     beforeInsert(this.CM);
    }
    m_CM.AddNew();
    if (afterInsert!=null)
    {
     afterInsert(this.CM);
    }
   }
  }
  /// <summary>
  /// 删除一条记录
  /// </summary>
  public void Del()
  {
   if (m_CM!=null)
   {
    if (m_CM.Position<0)
     return;
    if (BaseForm.ShowQuestionBox("确定要删除当前记录吗?")==true)
    {
     bool Cancel=true;
     if (beforeDelete!=null)
     {
      Cancel= beforeDelete(this.CM);
     }
     if (Cancel)
     {
      m_CM.RemoveAt(m_CM.Position);
      if (afterDelete!=null)
       afterDelete(this.CM);
     }
    
    }
   }
  }


  /// <summary>
  /// 将数据提交到缓存中
  /// </summary>
  public void Post()
  {
   bool isCancel=true;
   if (m_CM!=null)
   {
    if (beforePost!=null)
    {
     isCancel= beforePost(this.CM);
    }
    if(isCancel)
    {
     m_CM.EndCurrentEdit();
     if (afterPost!=null)
     {
      afterPost(this.CM);
     }
    }
   
   }
  }


  /// <summary>
  /// 取消当前编辑数据
  /// </summary>
  public void Cancel()
  {
   if (m_CM!=null)
   {
    if (beforeCancel!=null)
     beforeCancel(this.CM);
   
    if (BaseForm.ShowQuestionBox("需要撤消到初始状态吗?"))
    {
     DataRowView rv= m_CM.Current as DataRowView;
     rv.Row.RejectChanges();
    }
    else
    {
     m_CM.CancelCurrentEdit();
    }
    if (afterCancel!=null)
     afterCancel(this.CM);
   }
  }


  /// <summary>
  /// 下一个
  /// </summary>
  public void Next()
  {
   if (m_CM!=null)
   {
    if (m_CM.Position<m_CM.Count-1)
    {
     m_CM.Position+=1;
    }
   }
  }
  /// <summary>
  /// 上一个
  /// </summary>
  public void Previous()
  {
   if (m_CM!=null)
   {
    if(m_CM.Position > 0)
    {
     m_CM.Position-=1;
    }
   }
  
  }

  /// <summary>
  /// 第一个
  /// </summary>
  public void First()
  {
   if (m_CM!=null)
   {
    m_CM.Position=0;
   }
  }

  /// <summary>
  /// 最后一个
  /// </summary>
  public void Last()
  {
   if (m_CM!=null)
   {
    m_CM.Position=m_CM.Count-1;
   }
  }

  /// <summary>
  /// 下一页10
  /// </summary>
  public void NextPage()
  {
   if (m_CM!=null)
   {
    if ((m_CM.Position+20) <(m_CM.Count-1))
    {
     m_CM.Position+=20;
    }
    else
    {
     m_CM.Position=m_CM.Count-1;
    }
   }
           
  }

  /// <summary>
  /// 上一页
  /// </summary>
  public void PreviousPage()
  {
   if (m_CM!=null)
   {
    if ((m_CM.Position-20)>0)
    {
     m_CM.Position-=20;
    }
    else
    {
     m_CM.Position=0;
    }
   }

  }


  /// <summary>
  /// 记录指针移动
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void CM_PositionChanged(object sender, EventArgs e)
  {
   try
   {
      if (m_EditCount!=null)
    {
     m_EditCount.Caption=this.m_CM.Count.ToString();
    }
    int oldPosition=0;
    string strPosition="";
    if (m_EditCurrentNum!=null)
    {
     strPosition=m_EditCurrentNum.Caption;
    }
    if (strPosition.Trim()!="")
    {
     oldPosition=Convert.ToInt32(strPosition);
    }
    else
    {
     oldPosition=0;
    }
 
 
    if (m_EditCurrentNum!=null)
    {
     int intcur=this.m_CM.Position+1;
     m_EditCurrentNum.Caption=intcur.ToString();
     
    }
    if (cursorChange!=null)  //aCount==this.m_CM.Count
    {
     cursorChange(this.m_CM.Position,oldPosition-1,m_CM);
    }
    #region 系统状态行

    if(this.m_CM.Count == 0)
     return;
    if(this.m_CM.Position == -1)
     return;
    if (m_DataPanel==null)
    {
     return ;
    }
    DataRowView drv = m_CM.Current as DataRowView;
    if(drv == null)
     return;
    DataTable table = drv.Row.Table;
    DataTable dtdel = table.GetChanges(DataRowState.Deleted);
    DataTable dtins = table.GetChanges(DataRowState.Added);
    DataTable dtupd = table.GetChanges(DataRowState.Modified);
    if((dtdel == null) && (dtins == null) && (dtupd == null))
     m_DataPanel.Text = "无修改";
    else
    {
     int del = 0, ins = 0, upd = 0;
     if(dtdel != null)
      del = dtdel.Rows.Count;
     if(dtins != null)
      ins = dtins.Rows.Count;
     if(dtupd != null)
      upd = dtupd.Rows.Count;
     m_DataPanel.Text = string.Format("新增:{0} 更改:{1} 删除:{2}", ins, upd, del);
    }
    #endregion

   }
   catch(Exception ex)
   {
    BaseForm.ShowErrorBox(ex.Message);
   }
  }


  /// <summary>
  /// 保存数据
  /// </summary>
  public void Save()
  {
   if (SaveMethod!=null)
   {
    SaveMethod(m_CM);
   }
  }

  /// <summary>
  /// 获取全部数据
  /// </summary>
  public void GetAll()
  {
   if (GetAllDataMethod!=null)
   {
    GetAllDataMethod();
   }
  }

  /// <summary>
  ///
  /// </summary>
  /// <returns></returns>
  public bool isAvaildCurrent()
  {
   if (m_CM != null)
   {
    if (m_CM.Count > 0)
    {
     if (m_CM.Current != null)
     {
      return true;
     }
     else
     {
      return false;
     }
    }
    else
    {
     return false;
    }
   
   }
   else
   {
    return false;
   }
  }

  /// <summary>
  /// 保存方法
  /// </summary>
  public SaveData SaveMethod;

  /// <summary>
  /// 获取全部数据的方法
  /// </summary>
  public GetAllData GetAllDataMethod;
 


  #region 事件
  /// <summary>
  /// 提交前
  /// </summary>
  public event  BeforePost beforePost;
 
    //  private AfterPost m_afterPost=null;
  /// <summary>
  /// 提交后
  /// </summary>
  public event AfterPost afterPost;
   /// <summary>
  /// 插入前
  /// </summary>
  public event  BeforeInsert beforeInsert;
    /// <summary>
  /// 插入后
  /// </summary>
  public event AfterInsert afterInsert;
 

  /// <summary>
  /// 删除前
  /// </summary>
  public event BeforeDelete beforeDelete;


    /// <summary>
  /// 删除后
  /// </summary>
  public event AfterDelete afterDelete;
     /// <summary>
  /// 取消后
  /// </summary>
  public event AfterCancel afterCancel;
  /// <summary>
  /// 取消前
  /// </summary>
  public event BeforeCancel beforeCancel;
   /// <summary>
  /// 记录移动时
  /// </summary>
  public event CursorChange cursorChange;
  #endregion


 
 
 
 }


---------------------------------------------
 /// <summary>
 /// 添加空记录前发生
 /// </summary>
 public delegate void BeforeInsert(CurrencyManager sender);
 /// <summary>
 /// 添加空记录后发生
 /// </summary>
 public delegate void AfterInsert(CurrencyManager sender);
 /// <summary>
 /// 提交后发生
 /// </summary>
 public delegate void AfterPost(CurrencyManager sender);
 /// <summary>
 /// 提交前发生
 /// </summary>
 public delegate bool BeforePost(CurrencyManager sender);
 /// <summary>
 /// 删除前发生
 /// </summary>
 public delegate bool BeforeDelete(CurrencyManager sender);
 /// <summary>
 /// 删除后发生
 /// </summary>
 public delegate void AfterDelete(CurrencyManager sender);
 /// <summary>
 /// 取消前发生
 /// </summary>
 public delegate void BeforeCancel(CurrencyManager sender);
 /// <summary>
 /// 取消后发生
 /// </summary>
 public delegate void AfterCancel(CurrencyManager sender);


 /// <summary>
 /// 保存方法
 /// </summary>
 public delegate void SaveData(CurrencyManager sender);

 public delegate void GetAllData();
 


 public delegate void CursorChange(int Position,int oldPosition,CurrencyManager sender);

posted @ 2007-01-14 16:13  sunney  阅读(500)  评论(0编辑  收藏  举报