【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 ...
随机推荐
- LeetCode: Best Time to Buy and Sell Stock II [122]
[题目] Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- SVN & Git (一)
(一)SVN的使用.CornerStone图形化管理工具! SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS. ...
- 如何快速正确的安装 Ruby, Rails 运行环境
如何快速正确的安装 Ruby, Rails 运行环境 https://ruby-china.org/wiki/install_ruby_guide 对于新入门的开发者,如何安装 Ruby, Ruby ...
- [CLR via C#]1.3 加载公共语言运行时
原文:[CLR via C#]1.3 加载公共语言运行时 1. 你生成的每个程序集可以是EXE,也可以是DLL.最终都是有CLR管理这些程序集中代码的执行. 2. VS2010中,创建新的EXE项目时 ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- APP 半自适应 WEB页面
特别赶,响应式纯自适应的,有空写了新的发. (在手机上看,页面上看一定乱) <!DOCTYPE html><html> <head> <meta http-e ...
- Android-Launcher开发之ShortCut(1)
下面源代码来自Launcher2.3的样例 1.默认每一个应用的主Activity都会自带 <category android:name="android.intent.categor ...
- 查询DBlink创建
DBlink创建 查询 博客分类: Oracle 当用户要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中必须创建了远程数据库的dblink,通过dblink本地数据库可以像访问本地数据 ...
- jQuery库(noConflict)冲突解决机制
很多JSFramework库选择使用$符号作为一个函数或变量名,而在实际的项目开发,模板语言,则有可能"$"符号是模板语言keyword.例如Veclocity模板语言,$它是ke ...
- C#中使用REDIS
C#中使用REDIS 上一篇>> 摘要 上一篇讲述了安装redis客户端和服务器端,也大体地介绍了一下redis.本篇着重讲解.NET4.0 和 .NET4.5中如何使用redis和C# ...