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
{
    ///


    ///
    ///

    public class PagingCtrl
    {
        ///
        /// 页大小
        ///

        private int _PageSize = 50;

        ///


        /// 页索引
        ///

        private int _PageIndex = 1;

        ///


        /// 记录数
        ///

        private int _RecodeCount = 0;

        ///


        /// 页数
        ///

        private int _PageCount = 0;

        ///


        /// 回调函数获取要查询数据的XML数据参数
        ///

        ///
        ///
        ///
        public delegate string LoadXML(int PageIndex, int PageSize);

        ///


        /// 在读取数据后发生的事件,用于通知客户
        ///

        public delegate void afterDataLoadDelegate();

        ///


        /// 在读取数据前发生的事件,提示客户等待
        ///

        public delegate void beforeDataLoadDelegate();

        ///


        /// 在读取数据发生错误时发生,用于错误处理
        ///

        public delegate void errorDataLoadDelegate(string ErrMSG);

        ///


        /// 首页按钮
        ///

        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;

        ///


        /// 记录数提示Label
        ///

        System.Windows.Forms.Label _msg;

        ///


        /// 每页记录数combo
        ///

        System.Windows.Forms.ComboBox _comPageSize;

        ///


        /// 当前页索引combo
        ///

        System.Windows.Forms.ComboBox _comToPageIndex;

        ///


        /// 显示数据的datagridview
        ///

        C1.Win.C1FlexGrid.C1FlexGrid _dv;

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

        public string _webseiviceComm = "";

        ///


        /// 构造函数
        ///

        /// 首页
        /// 上一页
        /// 下一页
        /// 末页
        /// 刷新
        /// 共x页,记录数:x
        /// 页大小
        /// 页索引
        /// 显示数据的datagridview
        /// 命令编号
        /// 参数
        /// 加载之前处理的回调函数
        /// 加载之后处理的回调函数
        /// 错误处理的回调函数
        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);
            }
        }

        ///


        /// 调用webservice 方法获取数据
        ///

        ///
        ///
        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 = "";
            }
        }

        ///


        ///
        ///

        ///
        public void LoadData(string XmlComm)
        {
            LoadData(_webseiviceComm, XmlComm);

        }

        ///


        ///
        ///

        public void LoadData()
        {
            LoadData(_webseiviceComm, _loadxml(this._PageIndex, this._PageSize));
        }

        ///


        /// 加载web服务数据
        ///

        /// 命令编号
        /// 参数
        /// 数据集
        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);
            }
        }

        ///


        /// 刷新
        ///

        ///
        ///
        private void btReflesh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        ///


        /// 下一页
        ///

        public void NextPage()
        {
            this._btNext.PerformClick();
        }

        ///


        /// 下一页
        ///

        ///
        ///
        private void btNext_Click(object sender, EventArgs e)
        {
            _PageIndex++;
            LoadData();
        }

        ///


        /// 上一页
        ///

        public void UpPage()
        {
            this._btPreview.PerformClick();
        }

        ///


        /// 上一页
        ///

        ///
        ///
        private void btUp_Click(object sender, EventArgs e)
        {
            --_PageIndex;
            LoadData();
        }

        ///


        /// 首页
        ///

        ///
        ///
        private void btFirst_Click(object sender, EventArgs e)
        {
            _PageIndex = 1;
            LoadData();
        }

        ///


        /// 首页
        ///

        public void FirstPage()
        {
            this._btFirst.PerformClick();
        }

        ///


        /// 最后一页
        ///

        ///
        ///
        private void btLast_Click(object sender, EventArgs e)
        {
            _PageIndex = _PageCount;
            LoadData();
        }

        ///


        /// 最后一页
        ///

        public void LastPage()
        {
            this._btLast.PerformClick();
        }

        ///


        /// combobox选择改变页号
        ///

        ///
        ///
        private void comPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._PageSize = Int32.Parse(_comPageSize.Text);
            LoadData();
        }

        ///


        /// 翻到第几页
        ///

        ///
        ///
        private void comToPageIndex_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._PageIndex = Int32.Parse(_comToPageIndex.Text);
            LoadData();
        }
    }
}

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

你可能感兴趣的:(C#简单的通用基础字典实现方法)