【NET】Winform用户控件的初步封装之列表页控件
public abstract partial class TListPager<TEntity, TRepository, TSqlStrConstruct> : UserControl
where TEntity:Yom.Extend.Entity.EntityBase
where TRepository : Yom.Extend.Repository.RepositoryBaseRepository<TEntity, TSqlStrConstruct>
where TSqlStrConstruct : Huawei.Data.SqlStrConstruct
{
protected TRepository repository = System.Activator.CreateInstance<TRepository>();
ToolTip toolTip;
TSqlStrConstruct sqlStrConstruct;
public TListPager()
{
InitializeComponent();
if (this.IsContextMenu)
{
this.lvList.ContextMenuStrip = this.cmsOperation;
}
this.wfpPager.PageSize = ;
this.wfpPager.PageChanged += new WinFormPager.PageChangeDelegate(
() =>
{
DataBind();
}
);
this.Dock = System.Windows.Forms.DockStyle.Fill;
this.lvList.Dock = System.Windows.Forms.DockStyle.Top;
this.lvList.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lvList.ItemMouseHover += new ListViewItemMouseHoverEventHandler((object o, ListViewItemMouseHoverEventArgs ea) => {
if (!string.IsNullOrEmpty(ea.Item.ToolTipText)) {
if (this.toolTip == null) {
this.toolTip = new ToolTip();
}
this.toolTip.SetToolTip(this.lvList, ea.Item.ToolTipText);
}
});
this.cSearcher = this.CSearcher;
if (this.cSearcher == null)
{
this.pSearchContainer.Visible = false;
this.lvList.Location = new Point(, );
//this.lvList.Height += this.pSearchContainer.Height;
}
else {
if (this.cSearcher.SearchAction == null)
{
this.cSearcher.SearchAction = (SearchArgs sa) =>
{
if (sa != null)
{
if (sa.Params != null)
{
this.SearchParams = sa.Params.ToArray();
}
string whereStr = sa.SearchStr.ToString();
if (!string.IsNullOrEmpty(whereStr))
{
if (string.IsNullOrEmpty(this.Where))
{
this.where = sa.SearchStr.ToString();
}
else
{
this.where = string.Format("{0} AND {1}", this.Where, sa.SearchStr.ToString());
}
} }
TListPager_Load(null, null);
};
}
this.cSearcher.Dock = DockStyle.Fill;
this.pSearchContainer.Height = this.cSearcher.Height;
this.pSearchContainer.Controls.Add(this.cSearcher);
this.lvList.Location = new Point(, this.pSearchContainer.Height);
this.lvList.Height = this.Height - this.cSearcher.Height - this.wfpPager.Height;
}
editor = this.Editor;
Init();
}
protected virtual void Init(){ }
protected TEditorBase<TEntity, TRepository, TSqlStrConstruct> editor;
protected abstract TEditorBase<TEntity, TRepository, TSqlStrConstruct> Editor
{
get;
}
protected void TListPager_Load(object sender, EventArgs e)
{
this.wfpPager.CurrentPage = ;
DataBind();
}
protected abstract string Order
{
get; }
protected bool IsContextMenu {
get {
return true;
}
}
protected string where;
protected abstract string Where
{
get;
}
protected virtual System.Data.IDataParameter[] SearchParams
{
get;
set;
}
protected abstract ColumnHeader[] Columns
{
get;
}
protected Type EntityType {
get {
return typeof(TEntity);
}
}
protected virtual bool EnabledPage
{
get {
return true;
}
}
public virtual void DataBind(int pageIndex) {
this.wfpPager.CurrentPage = pageIndex;
DataBind();
}
private void DataBind()
{
int count = ;
TEntity[] tes;
if (string.IsNullOrEmpty(this.where)) {
this.where = this.Where;
}
if (EnabledPage)
{
tes = this.repository.FindAll(this.wfpPager.CurrentPage, this.wfpPager.PageSize, this.where, string.IsNullOrEmpty(this.Order)?string.Empty:this.Order, SearchParams, out count) as TEntity[];
}
else {
tes = this.repository.FindAll(where, Order, this.SearchParams) as TEntity[];
}
this.lvList.Columns.Clear();
this.lvList.Items.Clear();
this.lvList.Columns.Add(new ColumnHeader(){ Text="序号",Width= }); this.lvList.Columns.AddRange(this.Columns);
this.lvList.ShowItemToolTips = true;
int baseNo = this.wfpPager.PageSize * (this.wfpPager.CurrentPage - );
string[] itemArr ;
object propertyValue;
int columnIndexRecord;
ListViewItem toAdditem;
foreach (TEntity te in tes)
{
itemArr = new string[this.Columns.Length + ];
itemArr[] = (++baseNo).ToString();
columnIndexRecord = ;
foreach (ColumnHeader ch in this.Columns) {
columnIndexRecord++;
if (string.IsNullOrEmpty(ch.Name)) {
continue;
}
try
{
propertyValue = EntityType.GetProperty(ch.Name).GetValue(te, null);
}
catch {
continue;
}
if (propertyValue is DateTime)
{
itemArr[columnIndexRecord] = DateTimeAction(ch.Name, (DateTime)propertyValue);
}
else
{
itemArr[columnIndexRecord] = propertyValue.ToString();
} } toAdditem = new ListViewItem(itemArr) { Name = EntityType.GetProperty(this.repository.GetEntityKeyName()).GetValue(te, null).ToString() };
if (!string.IsNullOrEmpty(this.ItemTooltipFiledName))
{
toAdditem.ToolTipText = EntityType.GetProperty(this.ItemTooltipFiledName).GetValue(te, null).ToString();
}
toAdditem.Tag = te;
ItemBeforeAdd(toAdditem); this.lvList.Items.Add(toAdditem); }
if (EnabledPage)
{
this.wfpPager.RecordCount = count;
if (this.wfpPager.PageTotal.Equals() || count <= )
{ this.wfpPager.Visible = false;
if (this.cSearcher == null)
{
this.lvList.Height = this.Height;
}
else
{
this.lvList.Height = this.Height - this.cSearcher.Height;
}
}
else {
this.wfpPager.Visible = true;
if (this.cSearcher == null)
{
this.lvList.Height = this.Height - this.wfpPager.Height;
}
else
{
this.lvList.Height = this.Height - this.cSearcher.Height -this.wfpPager.Height;
}
}
}
else {
this.wfpPager.Visible = false;
if (this.cSearcher == null)
{
this.lvList.Height = this.Height;
}
else
{
this.lvList.Height = this.Height - this.cSearcher.Height;
} }
}
protected virtual void ItemBeforeAdd(ListViewItem toAdditem)
{ }
protected virtual string ItemTooltipFiledName
{
get;
set;
} protected virtual string DateTimeAction(string propertyName,DateTime dt) {
try
{
if (dt.Equals(DateTime.MinValue)) {
return string.Empty;
}
return dt.ToString("yyyy-MM-dd HH:mm");
}
catch {
try
{
return dt.ToString();
}
catch
{
return string.Empty;
}
//return string.Empty;
}
} protected virtual void cmsOperation_Opening(object sender, CancelEventArgs e)
{
if (this.editor == null && this.cmsOperation.Items.Count.Equals()) {
this.cmsOperation.Items.RemoveAt();
this.cmsOperation.Items.RemoveAt();
this.cmsOperation.Items.RemoveAt();
}
if (this.cmsOperation.Items.Count.Equals())
{
if (this.lvList.SelectedItems.Count <= )
{
this.cmsOperation.Items[].Enabled = this.cmsOperation.Items[].Enabled = false;
}
else
{
this.cmsOperation.Items[].Enabled = this.cmsOperation.Items[].Enabled = true;
}
}
else {
if (this.lvList.SelectedItems.Count <= )
{
this.cmsOperation.Items[].Enabled = false;
}
else
{
this.cmsOperation.Items[].Enabled = true;
}
}
}
protected virtual bool DeleteValid(out string msg) {
msg = string.Empty;
return true;
}
protected virtual void cmsOperation_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
this.cmsOperation.Visible = false;
string key = e.ClickedItem.Tag == null ? e.ClickedItem.Text : e.ClickedItem.Tag.ToString();
switch (key)
{
case "添加":
//if (this.cmsOperation.Tag != null)
//{
// (this.cmsOperation.Tag as Button).PerformClick();
//}
//else
//{
// (this.Parent.Parent.Controls.Find("Edit", true)[0] as Button).PerformClick();
//}
editor = this.Editor;
if (this.editor == null) {
return;
} this.Hide();
this.Parent.Controls.Add(this.editor);
break;
case "编辑":
//Button b;;
//if (this.cmsOperation.Tag != null)
//{
// b = this.cmsOperation.Tag as Button;
//}
//else
//{
// b = (this.Parent.Parent.Controls.Find("Edit", true)[0] as Button);
//}
//b.Tag = this.lvList.FocusedItem.Name;
//b.PerformClick();
editor = this.Editor;
if (this.Editor == null)
{
return;
} if (this.lvList.SelectedItems.Count > ) {
MessageBox.Show("只能选择一项!");
return;
}
if (this.lvList.SelectedItems.Count < )
{
MessageBox.Show("至少选择一项!");
return;
}
this.Hide();
this.Parent.Controls.Add(this.editor);
this.editor.Key = this.lvList.SelectedItems[].Name;
break;
case "删除":
try
{
if (!Huawei.PortableComputer.Log.LogVar.UserName.Equals("admin"))
{
MessageBox.Show("只有超级管理员才有删除记录的权限。");
return;
}
}
catch { }
string msg;
if (!DeleteValid(out msg)) {
if (!string.IsNullOrEmpty(msg)) {
MessageBox.Show(msg);
return;
}
}
if (DialogResult.OK.Equals(MessageBox.Show("确定要删除吗?", "通知", MessageBoxButtons.OKCancel)))
{
TEntity entity;//= System.Activator.CreateInstance<TEntity>();
if (sqlStrConstruct == null)
{
sqlStrConstruct = System.Activator.CreateInstance<TSqlStrConstruct>();
}
foreach (ListViewItem item in this.lvList.SelectedItems)
{
//this.EntityType.GetProperty(this.repository.GetEntityKeyName()).SetValue(entity, item.Name, null);
entity = this.repository.FindBy(item.Name) as TEntity;
this.repository.Delete(entity);
if (sqlStrConstruct.TableName.Equals("TB_SYS_LOG"))
{
continue;
}
Log.LogWriter.Write(string.Format("删除了信息,表为{0},主键为:{1}", sqlStrConstruct.TableName, item.Name));
}
TListPager_Load(sender, e);
}
break;
}
}
CSearcher cSearcher;
protected abstract CSearcher CSearcher
{
get;
}
}
【NET】Winform用户控件的初步封装之列表页控件的更多相关文章
- 【NET】Winform用户控件的初步封装之编辑控件
编辑控件 public abstract partial class TEditorBase <TEntity, TRepository, TSqlStrConstruct> : User ...
- VS2010/MFC编程入门之二十四(常用控件:列表框控件ListBox)
前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法.本文要讲的是列表框控件(ListBox)及其使用实例. 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选 ...
- VS2010-MFC(常用控件:列表框控件ListBox)
转自:http://www.jizhuomi.com/software/186.html 列表框控件简介 列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选中的项会高亮显示.列表框可分为单 ...
- MFC常用控件之列表视图控件(List Control)
近期学习了鸡啄米大神的博客,对其中的一些知识点做了一些自己的总结.不过,博客内容大部分来自鸡啄米.因此,这个博客算是转载博客,只是加了一些我自己的理解而已.若想学习鸡啄米大神的博客总结,请点击连接:h ...
- winform用户控件
用途用户控件包含Time控件和一个lable控件,一个ToolStrip控件,每隔一秒显示一次时间 1. 生成用户控件 新建一个项目类型为用户控件 注意定义类名,此类名为以后工具箱中显 ...
- WinForm用户控件、动态创建添加控件、timer控件--2016年12月12日
好文要顶 关注我 收藏该文 徐淳 关注 - 1 粉丝 - 3 0 0 用户控件: 通过布局将多个控件整合为一个控件,根据自己的需要进行修改,可对用户控件内的所有控件及控件属性进行修 ...
- winform用户控件、动态创建添加控件、timer控件、控件联动
用户控件: 相当于自定义的一个panel 里面可以放各种其他控件,并可以在后台一下调用整个此自定义控件. 使用方法:在项目上右键.添加.用户控件,之后用户控件的编辑与普通容器控件类似.如果要在后台往窗 ...
- FileUpload控件使用初步
FileUpload控件使用初步 FileUpload控件使用初步: 1.实现文件上传 protected void btnSubmit_click(object sender, EventArg ...
- Winform开发中如何将数据库字段绑定到ComboBox控件
最近开始自己动手写一个财务分析软件,由于自己也是刚学.Net不久,所以自己写的的时候遇到了很多问题,希望通过博客把一些印象深刻的问题记录下来. Winform开发中如何将数据库字段绑定到ComboBo ...
随机推荐
- 表现层及ASP.NET MVC介绍(二)
表现层及ASP.NET MVC介绍(二) 最近的更新速度越来越慢,主要是项目上比较忙,封装EasyUi也要花很多时间.不过大家请放心,本系列不会半途夭折,并且代码干货也会持续更新.本文继续介绍表现层和 ...
- MVC验证03-自定义验证规则、禁止输入某些值
原文:MVC验证03-自定义验证规则.禁止输入某些值 本文继续体验自定义验证规则,需求是禁止输入某些值.本文与前2篇相关,请参考:MVC验证01-基础.远程验证 MVC验证02-自定义验证规则.邮 ...
- 如何设置Installshield中 feature的选中状态
原文:如何设置Installshield中 feature的选中状态 上一篇: 使用strtuts2的iterator标签循环输出二维数组之前一直有筒子问如何设置Installshield中 feat ...
- MySQL安装指南
近期领导突然说要用MySQL,我立刻当天晚上就研究了一下. http://www.mysql.com/这是官网,还好能够訪问.好多年前已经被oracle收购.分为企业版和社区版: MySQL Ente ...
- VS2015预览
VS2015预览版体验 .NET开源了,JAVA颤抖吧...据说VS2015可以开发android,ios,wp应用程序了,还可以开发能运行在mac,linux上的ASP.NET网站,如果真是这样 ...
- 全球最快的JS模板引擎:tppl
废话不多说,先上测试: 亲测请访问:[在线测试地址]单次结果不一定准确,请多测几次. tppl 的编译渲染速度是著名的 jQuery 作者 John Resig 开发的 tmpl 的 43 倍!与第二 ...
- C#在outlook里创建一封邮件到草稿箱
原文:C#在outlook里创建一封邮件到草稿箱 1.引用Microsoft.Office.Interop.Outlook.dll 2. 实现代码 public static int SendToD ...
- 由浅入深学习.NET CLR 系列:目录
经过对Android的一阵折腾,些许熟悉了一些Java的东东,又开始转战.NET.我觉得学习最好和工作不要相离太远,才会更加随笔随意,索性整理一些比较系统的.NET的基础知识学习学习.一提起学习.NE ...
- [Android] 获取WebView的页面标题(Title)-----WebChromeClient.onReceivedTitle()方法的重写
应用开发中需要获取WebView当前页面的标题,可能通过对WebChromeClient.onReceivedTitle()方法的重写来实现 效果图如下: 代码如下: public class Mai ...
- Javascript Array API
JS数组对象提供了很多API方法,由于前段时间要用到某一些方法,但是突然一时又想不起来该怎么用了,上网找有很多资料都不全,所以就自己整理了一篇,完全是自己写的的JS,只是复制到这里来了 ,要用到的朋友 ...