欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-15 08:36:52
本文实例讲述了c#简单的通用基础字典实现方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下: using system; using system.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#程序设计有所帮助。

上一篇:

下一篇: