📄 databindinghelper.cs
字号:
using System;
using System.Windows.Forms;
using System.Data;
namespace PublicUIComponent
{
/// <summary>
/// 用于帮助实现数据绑定控件的许多功能。
/// </summary>
public class DataBindingHelper
{
public DataBindingHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//绑定管理器
CurrencyManager _cm = null;
//绑定控件容器
Control _container = null;
/// <summary>
/// 返回CurrencyManager对象
/// </summary>
public CurrencyManager CM
{
get
{
return _cm;
}
set
{
_cm = value;
}
}
/// <summary>
/// 返回绑定管理器的位置
/// </summary>
public int CmPosition
{
get
{
if(_cm != null)
return _cm.Position;
else
return -1;
}
}
/// <summary>
/// 返回绑定管理器的记录数
/// </summary>
public int CmCount
{
get
{
if(_cm != null)
return _cm.Count;
else
return 0;
}
}
/// <summary>
/// 容器控件
/// </summary>
public Control Container
{
get
{
return _container;
}
set
{
_container = value;
}
}
/// <summary>
/// 设定绑定数据源
/// </summary>
/// <param name="dataSource">数据集</param>
/// <param name="dataMember">数据表</param>
public void SetDataSource(object dataSource,string dataMember)
{
if(_container == null)
{
MessageBox.Show("请先指定容器控件!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
if(dataMember == null)
{
_cm = _container.BindingContext[dataSource] as CurrencyManager;
}
else
{
_cm = _container.BindingContext[dataSource,dataMember] as CurrencyManager;
}
}
/// <summary>
/// 移动到首记录
/// </summary>
public void MoveFirst()
{
if(_cm == null || _cm.Count == 0)
return;
_cm.Position = 0;
}
/// <summary>
/// 移动到上一条记录
/// </summary>
public void MovePrev()
{
if(_cm == null || _cm.Count == 0)
return;
if(_cm.Position > 0)
_cm.Position -= 1;
}
/// <summary>
/// 移动到下一条记录
/// </summary>
public void MoveNext()
{
if(_cm == null || _cm.Count == 0)
return;
if(_cm.Position < _cm.Count-1)
_cm.Position += 1;
}
/// <summary>
/// 移动到尾记录
/// </summary>
public void MoveLast()
{
if(_cm == null || _cm.Count == 0)
return;
_cm.Position = _cm.Count-1;
}
/// <summary>
/// 移动到指定的位置
/// </summary>
/// <param name="iRow"></param>
public void MoveToPosition(int iRow)
{
if(_cm == null || _cm.Count == 0 || iRow >= _cm.Count || iRow < -1)
return;
_cm.Position = iRow;
}
/// <summary>
/// 取出当前数据源当前行指定字段的值,注意,主要针对DataTable
/// </summary>
/// <param name="FieldName"></param>
/// <returns></returns>
public object GetDataTableFieldValue(string FieldName)
{
if(_cm == null || _cm.Count == 0 || _cm.Current == null)
return null;
object retObj = ((DataRowView)_cm.Current)[FieldName];
return retObj;
}
/// <summary>
/// 删除当前行
/// </summary>
public void DeleteCurrentDataRow()
{
if(_cm == null || _cm.Count == 0)
return;
if(_cm.Position != -1
&& MessageBox.Show("您真的要删除当前这条记录吗?","提示信息",MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK)
_cm.RemoveAt(_cm.Position);
}
/// <summary>
/// 获取当前数据行
/// </summary>
/// <returns></returns>
public DataRow GetCurrentDataRow()
{
if(_cm == null || _cm.Count == 0 || _cm.Current == null)
return null;
return ((DataRowView)_cm.Current).Row;
}
/// <summary>
/// 获取指定序号(即第几行,从0起)的DataRow
/// </summary>
/// <param name="iRow"></param>
/// <returns></returns>
public DataRow GetDataRowFromRowIndex(int iRow)
{
if(_cm == null || _cm.Count == 0)
return null;
if(iRow <= -1 || iRow >= _cm.Count)
return null;
return ((DataRowView)_cm.List[iRow]).Row;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -