C#简单的通用基础字典实现方法

 更新时间:2014年12月12日 10:37:40   转载 投稿:shichen2014  
这篇文章主要介绍了C#简单的通用基础字典实现方法,包含了字典的索引、记录、回调与查询等技巧,需要的朋友可以参考下

本文实例讲述了C#简单的通用基础字典实现方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Alif.Alif_WSAPI;
using System.Data;
using System.Windows.Forms;

namespace Alif.AlifForm.CommClass
{
    /// <summary>
    ///
    /// </summary>
    public class PagingCtrl
    {
        /// <summary>
        /// 页大小
        /// </summary>
        private int _PageSize = 50;

        /// <summary>
        /// 页索引
        /// </summary>
        private int _PageIndex = 1;

        /// <summary>
        /// 记录数
        /// </summary>
        private int _RecodeCount = 0;

        /// <summary>
        /// 页数
        /// </summary>
        private int _PageCount = 0;

        /// <summary>
        /// 回调函数获取要查询数据的XML数据参数
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public delegate string LoadXML(int PageIndex, int PageSize);

        /// <summary>
        /// 在读取数据后发生的事件,用于通知客户
        /// </summary>
        public delegate void afterDataLoadDelegate();

        /// <summary>
        /// 在读取数据前发生的事件,提示客户等待
        /// </summary>
        public delegate void beforeDataLoadDelegate();

        /// <summary>
        /// 在读取数据发生错误时发生,用于错误处理
        /// </summary>
        public delegate void errorDataLoadDelegate(string ErrMSG);

        /// <summary>
        /// 首页按钮
        /// </summary>
        System.Windows.Forms.Button _btFirst;

        /// <summary>
        /// 前页按钮
        /// </summary>
        System.Windows.Forms.Button _btPreview;

        /// <summary>
        /// 下一页按钮
        /// </summary>
        System.Windows.Forms.Button _btNext;

        /// <summary>
        /// 末页按钮
        /// </summary>
        System.Windows.Forms.Button _btLast;

        /// <summary>
        /// 刷新按钮
        /// </summary>
        System.Windows.Forms.Button _btreflesh;

        /// <summary>
        /// 记录数提示Label
        /// </summary>
        System.Windows.Forms.Label _msg;

        /// <summary>
        /// 每页记录数combo
        /// </summary>
        System.Windows.Forms.ComboBox _comPageSize;

        /// <summary>
        /// 当前页索引combo
        /// </summary>
        System.Windows.Forms.ComboBox _comToPageIndex;

        /// <summary>
        /// 显示数据的datagridview
        /// </summary>
        C1.Win.C1FlexGrid.C1FlexGrid _dv;

        LoadXML _loadxml;
        afterDataLoadDelegate _afterDataLoad;
        beforeDataLoadDelegate _beferDataLoad;
        errorDataLoadDelegate _errorDataLoad;

        public string _webseiviceComm = "";

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="btFirst">首页</param>
        /// <param name="btPreview">上一页</param>
        /// <param name="btNext">下一页</param>
        /// <param name="btLast">末页</param>
        /// <param name="btreflesh">刷新</param>
        /// <param name="msg">共x页,记录数:x</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="PageIndex">页索引</param>
        /// <param name="dv">显示数据的datagridview</param>
        /// <param name="webseiviceComm">命令编号</param>
        /// <param name="loadxmlCallBack">参数</param>
        /// <param name="beferfireCallBack">加载之前处理的回调函数</param>
        /// <param name="afterfireCallBack">加载之后处理的回调函数</param>
        /// <param name="errorfireCallBack">错误处理的回调函数</param>
        public PagingCtrl(System.Windows.Forms.Button btFirst,
                                    System.Windows.Forms.Button btPreview,
                                    System.Windows.Forms.Button btNext,
                                    System.Windows.Forms.Button btLast,
                                    System.Windows.Forms.Button btreflesh,
                                    System.Windows.Forms.Label msg,
                                    System.Windows.Forms.ComboBox PageSize,
                                    System.Windows.Forms.ComboBox PageIndex,
                                    C1.Win.C1FlexGrid.C1FlexGrid dv,
                                    string webseiviceComm,
                                    LoadXML loadxmlCallBack,
                                    beforeDataLoadDelegate beferfireCallBack,
                                    afterDataLoadDelegate afterfireCallBack,
                                    errorDataLoadDelegate errorfireCallBack)
        {
            _btFirst = btFirst;
            _btPreview = btPreview;
            _btNext = btNext;
            _btLast = btLast;
            _msg = msg;
            _dv = (C1.Win.C1FlexGrid.C1FlexGrid)dv;
            _loadxml = loadxmlCallBack;
            _webseiviceComm = webseiviceComm;
            _comPageSize = PageSize;
            _comToPageIndex = PageIndex;
            _btreflesh = btreflesh;
            _beferDataLoad = beferfireCallBack;
            _afterDataLoad = afterfireCallBack;
            _errorDataLoad = errorfireCallBack;

            if (_btFirst != null)
            {
                _btFirst.Click += new System.EventHandler(this.btFirst_Click);
            }
            if (_btPreview != null)
            {
                _btPreview.Click += new System.EventHandler(this.btUp_Click);
            }
            if (_btNext != null)
            {
                _btNext.Click += new System.EventHandler(this.btNext_Click);
            }
            if (_btLast != null)
            {
                _btLast.Click += new System.EventHandler(this.btLast_Click);
            }

            if (_comPageSize != null)
            {
                _comPageSize.SelectedIndexChanged += new System.EventHandler(comPageSize_SelectedIndexChanged);
            }
            if (_comToPageIndex != null)
            {
                _comToPageIndex.SelectedIndexChanged += new EventHandler(comToPageIndex_SelectedIndexChanged);
                _comToPageIndex.Tag = "";
            }
            if (_btreflesh != null)
            {
                _btreflesh.Click += new System.EventHandler(this.btReflesh_Click);
            }
        }

        /// <summary>
        /// 调用webservice 方法获取数据
        /// </summary>
        /// <param name="webservicecomm"></param>
        /// <param name="Xmlcommand"></param>
        public void LoadData(string webservicecomm, string Xmlcommand)
        {
            if (_comToPageIndex != null)
            {
                //防止程序重入
                if (_comToPageIndex.Tag == null || _comToPageIndex.Tag.ToString() == "1")
                {
                    return;
                }
                _comToPageIndex.Tag = "1";
            }

            //出发beforefire事件
            if (_beferDataLoad != null)
            {
                _beferDataLoad();
            }

            System.Data.DataSet ds;

            try
            {
                ds = LoadWebServiceData(webservicecomm, Xmlcommand);

                if (!ds.Tables.Contains("table0"))
                {
                    if(_dv.DataSource!=null)
                    {
                        ((DataTable)_dv.DataSource).Clear();
                    }
                }
                else
                {

                    _dv.DataSource = ds.Tables["table0"];


                }

                this._PageSize = Int32.Parse(ds.Tables["TabScript"].Rows[0]["PageSize"].ToString());
                this._PageIndex = Int32.Parse(ds.Tables["TabScript"].Rows[0]["PageIndex"].ToString());
                this._RecodeCount = Int32.Parse(ds.Tables["TabScript"].Rows[0]["Rows"].ToString());
                this._PageCount = this._RecodeCount / this._PageSize + 1;

                if (_comToPageIndex != null)
                {
                    _comToPageIndex.Items.Clear();
                    for (int i = 1; i <= this._PageCount; ++i)
                    {
                        _comToPageIndex.Items.Add(i.ToString());

                    }
                    _comToPageIndex.Text = this._PageIndex.ToString();
                }

                if (_msg != null)
                {
                    _msg.Text = "当前第" + this._PageIndex + "页," + "共" + this._PageCount.ToString() + "页,记录数:" + this._RecodeCount.ToString();
                }

                if (_btFirst != null)
                {
                    _btFirst.Enabled = this._PageIndex <= 1 ? false : true;
                }
                if (_btPreview != null)
                {
                    _btPreview.Enabled = _btFirst.Enabled;
                }
                if (_btLast != null)
                {
                    _btLast.Enabled = this._PageIndex >= this._PageCount ? false : true;
                }

                if (_btLast != null)
                {
                    _btLast.Enabled = _btLast.Enabled;
                }
            }
            catch (Exception ex)
            {
                //发生webservice错误
                if (_errorDataLoad != null)
                {
                    _errorDataLoad("连接WebService服务器失败(001)!\n\r错误原因:" + ex.Message);
                }
                if (_comToPageIndex != null)
                {
                    _comToPageIndex.Tag = "";
                }
            }

            if (_afterDataLoad != null)
            {
                _afterDataLoad();
            }

            if (_comToPageIndex != null)
            {
                _comToPageIndex.Tag = "";
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="XmlComm"></param>
        public void LoadData(string XmlComm)
        {
            LoadData(_webseiviceComm, XmlComm);

        }

        /// <summary>
        ///
        /// </summary>
        public void LoadData()
        {
            LoadData(_webseiviceComm, _loadxml(this._PageIndex, this._PageSize));
        }

        /// <summary>
        /// 加载web服务数据
        /// </summary>
        /// <param name="cmdID">命令编号</param>
        /// <param name="para">参数</param>
        /// <returns>数据集</returns>
        private DataSet LoadWebServiceData(string cmdID, string para)
        {
            int tt;
            string errmsg = "";
            System.Data.DataSet ds = new DataSet();
            try
            {
                tt = WebService.GetInstance(CommonInfo.LoginInfo.userParameters).Execute("DataServices", cmdID, "1", para, out errmsg, out  ds);
            }
            catch (Exception ex)
            {
                //发生webservice错误
                throw new Exception("连接WebService服务器失败(001)!\n\r错误原因:" + ex.Message);
            }

            if (tt == 1)
            {
                //获取数据成功
                return ds;
            }
            else
            {
                //获取keystring错误
                throw new Exception("服务器返回失败信息(002)!\n\r错误原因:" + errmsg);
            }
        }

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btReflesh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 下一页
        /// </summary>
        public void NextPage()
        {
            this._btNext.PerformClick();
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btNext_Click(object sender, EventArgs e)
        {
            _PageIndex++;
            LoadData();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        public void UpPage()
        {
            this._btPreview.PerformClick();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btUp_Click(object sender, EventArgs e)
        {
            --_PageIndex;
            LoadData();
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btFirst_Click(object sender, EventArgs e)
        {
            _PageIndex = 1;
            LoadData();
        }

        /// <summary>
        /// 首页
        /// </summary>
        public void FirstPage()
        {
            this._btFirst.PerformClick();
        }

        /// <summary>
        /// 最后一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btLast_Click(object sender, EventArgs e)
        {
            _PageIndex = _PageCount;
            LoadData();
        }

        /// <summary>
        /// 最后一页
        /// </summary>
        public void LastPage()
        {
            this._btLast.PerformClick();
        }

        /// <summary>
        /// combobox选择改变页号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._PageSize = Int32.Parse(_comPageSize.Text);
            LoadData();
        }

        /// <summary>
        /// 翻到第几页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comToPageIndex_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._PageIndex = Int32.Parse(_comToPageIndex.Text);
            LoadData();
        }
    }
}

希望本文所述对大家的C#程序设计有所帮助。

相关文章

  • c# 二分查找算法

    c# 二分查找算法

    折半搜索,也称二分查找算法、二分搜索,是一种在有序数组中查找某一特定元素的搜索算法
    2013-10-10
  • c# winform treelistview的使用(treegridview)实例详解

    c# winform treelistview的使用(treegridview)实例详解

    这篇文章主要介绍了c# winform treelistview的使用(treegridview),本文通过实例代码给大家详细介绍,需要的朋友可以参考下
    2017-12-12
  • 基于C#开发中的那些编码问题(详谈)

    基于C#开发中的那些编码问题(详谈)

    下面小编就为大家分享一篇基于C#开发中的那些编码问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-11-11
  • C#实现给DataGrid单元行添加双击事件的方法

    C#实现给DataGrid单元行添加双击事件的方法

    这篇文章主要介绍了C#实现给DataGrid单元行添加双击事件的方法,较为详细的分析了C#给DataGrid单元添加双击事件的步骤及相关实现代码,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • C#控制台模拟电梯工作原理

    C#控制台模拟电梯工作原理

    简单的模拟一下电梯的运行,电梯内部和外部楼层呼叫的优先级判断。以前学硬件的时候做这个不成问题,现在用软件来模拟对我来说比较难,要C#的图形界面。求高手赐教。
    2015-06-06
  • C#  ADO.NET 离线查询的实现示例

    C# ADO.NET 离线查询的实现示例

    这篇文章主要介绍了C# ADO.NET 离线查询的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • 如何解决hash冲突

    如何解决hash冲突

    上篇文章 为什么哈希存取比较快?使用它需要付出什么代价 只是简单介绍了使用hash所带来的利与弊。并未涉及hash的技术细节,本文则着重学习一下如何解决哈希编址的冲突问题。
    2016-06-06
  • C#中String StringBuilder StringBuffer类的用法

    C#中String StringBuilder StringBuffer类的用法

    这篇文章给大家简单介绍下C#中String StringBuilder StringBuffer三个类的用法,需要的的朋友参考下吧
    2017-05-05
  • WPF中窗体最大化问题的解决方法

    WPF中窗体最大化问题的解决方法

    这篇文章主要给大家介绍了关于WPF中窗体最大化问题的解决方法,文中通过示例代码介绍的非常详细,对大家学习或者使用wpf具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-10-10
  • 超简单C#获取带汉字的字符串真实长度(单个英文长度为1,单个中文长度为2)

    超简单C#获取带汉字的字符串真实长度(单个英文长度为1,单个中文

    正常情况下,我们是直接去string的length的,但是汉字是有两个字节的,所以直接用length是错的
    2018-03-03

最新评论