c# 简单的通用基础字典

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using Alif.Alif_WSAPI;
  6 using System.Data;
  7 using System.Windows.Forms;
  8 
  9 namespace Alif.AlifForm.CommClass
 10 {
 11     /// <summary>
 12     /// 
 13     /// </summary>
 14     public class PagingCtrl
 15     {
 16         /// <summary>
 17         /// 页大小
 18         /// </summary>
 19         private int _PageSize = 50;
 20 
 21         /// <summary>
 22         /// 页索引
 23         /// </summary>
 24         private int _PageIndex = 1;
 25 
 26         /// <summary>
 27         /// 记录数
 28         /// </summary>
 29         private int _RecodeCount = 0;
 30 
 31         /// <summary>
 32         /// 页数
 33         /// </summary>
 34         private int _PageCount = 0;
 35 
 36         /// <summary>
 37         /// 回调函数获取要查询数据的XML数据参数
 38         /// </summary>
 39         /// <param name="PageIndex"></param>
 40         /// <param name="PageSize"></param>
 41         /// <returns></returns>
 42         public delegate string LoadXML(int PageIndex, int PageSize);
 43 
 44         /// <summary>
 45         /// 在读取数据后发生的事件,用于通知客户
 46         /// </summary>
 47         public delegate void afterDataLoadDelegate();
 48 
 49         /// <summary>
 50         /// 在读取数据前发生的事件,提示客户等待
 51         /// </summary>
 52         public delegate void beforeDataLoadDelegate();
 53 
 54         /// <summary>
 55         /// 在读取数据发生错误时发生,用于错误处理
 56         /// </summary>
 57         public delegate void errorDataLoadDelegate(string ErrMSG);
 58 
 59         /// <summary>
 60         /// 首页按钮
 61         /// </summary>
 62         System.Windows.Forms.Button _btFirst;
 63 
 64         /// <summary>
 65         /// 前页按钮
 66         /// </summary>
 67         System.Windows.Forms.Button _btPreview;
 68 
 69         /// <summary>
 70         /// 下一页按钮
 71         /// </summary>
 72         System.Windows.Forms.Button _btNext;
 73 
 74         /// <summary>
 75         /// 末页按钮
 76         /// </summary>
 77         System.Windows.Forms.Button _btLast;
 78 
 79         /// <summary>
 80         /// 刷新按钮
 81         /// </summary>
 82         System.Windows.Forms.Button _btreflesh;
 83 
 84         /// <summary>
 85         /// 记录数提示Label
 86         /// </summary>
 87         System.Windows.Forms.Label _msg;
 88 
 89         /// <summary>
 90         /// 每页记录数combo
 91         /// </summary>
 92         System.Windows.Forms.ComboBox _comPageSize;
 93 
 94         /// <summary>
 95         /// 当前页索引combo
 96         /// </summary>
 97         System.Windows.Forms.ComboBox _comToPageIndex;
 98 
 99         /// <summary>
100         /// 显示数据的datagridview
101         /// </summary>
102         C1.Win.C1FlexGrid.C1FlexGrid _dv;
103 
104         LoadXML _loadxml;
105         afterDataLoadDelegate _afterDataLoad;
106         beforeDataLoadDelegate _beferDataLoad;
107         errorDataLoadDelegate _errorDataLoad;
108 
109         public string _webseiviceComm = "";
110 
111         /// <summary>
112         /// 构造函数
113         /// </summary>
114         /// <param name="btFirst">首页</param>
115         /// <param name="btPreview">上一页</param>
116         /// <param name="btNext">下一页</param>
117         /// <param name="btLast">末页</param>
118         /// <param name="btreflesh">刷新</param>
119         /// <param name="msg">共x页,记录数:x</param>
120         /// <param name="PageSize">页大小</param>
121         /// <param name="PageIndex">页索引</param>
122         /// <param name="dv">显示数据的datagridview</param>
123         /// <param name="webseiviceComm">命令编号</param>
124         /// <param name="loadxmlCallBack">参数</param>
125         /// <param name="beferfireCallBack">加载之前处理的回调函数</param>
126         /// <param name="afterfireCallBack">加载之后处理的回调函数</param>
127         /// <param name="errorfireCallBack">错误处理的回调函数</param>
128         public PagingCtrl(System.Windows.Forms.Button btFirst,
129                                     System.Windows.Forms.Button btPreview,
130                                     System.Windows.Forms.Button btNext,
131                                     System.Windows.Forms.Button btLast,
132                                     System.Windows.Forms.Button btreflesh,
133                                     System.Windows.Forms.Label msg,
134                                     System.Windows.Forms.ComboBox PageSize,
135                                     System.Windows.Forms.ComboBox PageIndex,
136                                     C1.Win.C1FlexGrid.C1FlexGrid dv,
137                                     string webseiviceComm,
138                                     LoadXML loadxmlCallBack,
139                                     beforeDataLoadDelegate beferfireCallBack,
140                                     afterDataLoadDelegate afterfireCallBack,
141                                     errorDataLoadDelegate errorfireCallBack)
142         {
143             _btFirst = btFirst;
144             _btPreview = btPreview;
145             _btNext = btNext;
146             _btLast = btLast;
147             _msg = msg;
148             _dv = (C1.Win.C1FlexGrid.C1FlexGrid)dv;
149             _loadxml = loadxmlCallBack;
150             _webseiviceComm = webseiviceComm;
151             _comPageSize = PageSize;
152             _comToPageIndex = PageIndex;
153             _btreflesh = btreflesh;
154             _beferDataLoad = beferfireCallBack;
155             _afterDataLoad = afterfireCallBack;
156             _errorDataLoad = errorfireCallBack;
157 
158             if (_btFirst != null)
159             {
160                 _btFirst.Click += new System.EventHandler(this.btFirst_Click);
161             }
162             if (_btPreview != null)
163             {
164                 _btPreview.Click += new System.EventHandler(this.btUp_Click);
165             }
166             if (_btNext != null)
167             {
168                 _btNext.Click += new System.EventHandler(this.btNext_Click);
169             }
170             if (_btLast != null)
171             {
172                 _btLast.Click += new System.EventHandler(this.btLast_Click);
173             }
174 
175             if (_comPageSize != null)
176             {
177                 _comPageSize.SelectedIndexChanged += new System.EventHandler(comPageSize_SelectedIndexChanged);
178             }
179             if (_comToPageIndex != null)
180             {
181                 _comToPageIndex.SelectedIndexChanged += new EventHandler(comToPageIndex_SelectedIndexChanged);
182                 _comToPageIndex.Tag = "";
183             }
184             if (_btreflesh != null)
185             {
186                 _btreflesh.Click += new System.EventHandler(this.btReflesh_Click);
187             }
188         }
189 
190         /// <summary>
191         /// 调用webservice 方法获取数据
192         /// </summary>
193         /// <param name="webservicecomm"></param>
194         /// <param name="Xmlcommand"></param>
195         public void LoadData(string webservicecomm, string Xmlcommand)
196         {
197             if (_comToPageIndex != null)
198             {
199                 //防止程序重入
200                 if (_comToPageIndex.Tag == null || _comToPageIndex.Tag.ToString() == "1")
201                 {
202                     return;
203                 }
204                 _comToPageIndex.Tag = "1";
205             }
206 
207             //出发beforefire事件
208             if (_beferDataLoad != null)
209             {
210                 _beferDataLoad();
211             }
212 
213             System.Data.DataSet ds;
214 
215             try
216             {
217                 ds = LoadWebServiceData(webservicecomm, Xmlcommand);
218 
219                 if (!ds.Tables.Contains("table0"))
220                 {
221                     if(_dv.DataSource!=null)
222                     {
223                         ((DataTable)_dv.DataSource).Clear();
224                     }
225                 }
226                 else
227                 {
228 
229                     _dv.DataSource = ds.Tables["table0"];
230 
231 
232                 }
233 
234                 this._PageSize = Int32.Parse(ds.Tables["TabScript"].Rows[0]["PageSize"].ToString());
235                 this._PageIndex = Int32.Parse(ds.Tables["TabScript"].Rows[0]["PageIndex"].ToString());
236                 this._RecodeCount = Int32.Parse(ds.Tables["TabScript"].Rows[0]["Rows"].ToString());
237                 this._PageCount = this._RecodeCount / this._PageSize + 1;
238 
239                 if (_comToPageIndex != null)
240                 {
241                     _comToPageIndex.Items.Clear();
242                     for (int i = 1; i <= this._PageCount; ++i)
243                     {
244                         _comToPageIndex.Items.Add(i.ToString());
245 
246                     }
247                     _comToPageIndex.Text = this._PageIndex.ToString();
248                 }
249 
250                 if (_msg != null)
251                 {
252                     _msg.Text = "当前第" + this._PageIndex + "页," + "" + this._PageCount.ToString() + "页,记录数:" + this._RecodeCount.ToString();
253                 }
254 
255                 if (_btFirst != null)
256                 {
257                     _btFirst.Enabled = this._PageIndex <= 1 ? false : true;
258                 }
259                 if (_btPreview != null)
260                 {
261                     _btPreview.Enabled = _btFirst.Enabled;
262                 }
263                 if (_btLast != null)
264                 {
265                     _btLast.Enabled = this._PageIndex >= this._PageCount ? false : true;
266                 }
267 
268                 if (_btLast != null)
269                 {
270                     _btLast.Enabled = _btLast.Enabled;
271                 }
272             }
273             catch (Exception ex)
274             {
275                 //发生webservice错误
276                 if (_errorDataLoad != null)
277                 {
278                     _errorDataLoad("连接WebService服务器失败(001)!

错误原因:" + ex.Message);
279                 }
280                 if (_comToPageIndex != null)
281                 {
282                     _comToPageIndex.Tag = "";
283                 }
284             }
285 
286             if (_afterDataLoad != null)
287             {
288                 _afterDataLoad();
289             }
290 
291             if (_comToPageIndex != null)
292             {
293                 _comToPageIndex.Tag = "";
294             }
295         }
296 
297         /// <summary>
298         /// 
299         /// </summary>
300         /// <param name="XmlComm"></param>
301         public void LoadData(string XmlComm)
302         {
303             LoadData(_webseiviceComm, XmlComm);
304 
305         }
306 
307         /// <summary>
308         /// 
309         /// </summary>
310         public void LoadData()
311         {
312             LoadData(_webseiviceComm, _loadxml(this._PageIndex, this._PageSize));
313         }
314 
315         /// <summary>
316         /// 加载web服务数据
317         /// </summary>
318         /// <param name="cmdID">命令编号</param>
319         /// <param name="para">参数</param>
320         /// <returns>数据集</returns>
321         private DataSet LoadWebServiceData(string cmdID, string para)
322         {
323             int tt;
324             string errmsg = "";
325             System.Data.DataSet ds = new DataSet();
326             try
327             {
328                 tt = WebService.GetInstance(CommonInfo.LoginInfo.userParameters).Execute("DataServices", cmdID, "1", para, out errmsg, out  ds);
329             }
330             catch (Exception ex)
331             {
332                 //发生webservice错误
333                 throw new Exception("连接WebService服务器失败(001)!

错误原因:" + ex.Message);
334             }
335 
336             if (tt == 1)
337             {
338                 //获取数据成功
339                 return ds;
340             }
341             else
342             {
343                 //获取keystring错误
344                 throw new Exception("服务器返回失败信息(002)!

错误原因:" + errmsg);
345             }
346         }
347 
348         /// <summary>
349         /// 刷新
350         /// </summary>
351         /// <param name="sender"></param>
352         /// <param name="e"></param>
353         private void btReflesh_Click(object sender, EventArgs e)
354         {
355             LoadData();
356         }
357 
358         /// <summary>
359         /// 下一页
360         /// </summary>
361         public void NextPage()
362         {
363             this._btNext.PerformClick();
364         }
365 
366         /// <summary>
367         /// 下一页
368         /// </summary>
369         /// <param name="sender"></param>
370         /// <param name="e"></param>
371         private void btNext_Click(object sender, EventArgs e)
372         {
373             _PageIndex++;
374             LoadData();
375         }
376 
377         /// <summary>
378         /// 上一页
379         /// </summary>
380         public void UpPage()
381         {
382             this._btPreview.PerformClick();
383         }
384 
385         /// <summary>
386         /// 上一页
387         /// </summary>
388         /// <param name="sender"></param>
389         /// <param name="e"></param>
390         private void btUp_Click(object sender, EventArgs e)
391         {
392             --_PageIndex;
393             LoadData();
394         }
395 
396         /// <summary>
397         /// 首页
398         /// </summary>
399         /// <param name="sender"></param>
400         /// <param name="e"></param>
401         private void btFirst_Click(object sender, EventArgs e)
402         {
403             _PageIndex = 1;
404             LoadData();
405         }
406 
407         /// <summary>
408         /// 首页
409         /// </summary>
410         public void FirstPage()
411         {
412             this._btFirst.PerformClick();
413         }
414 
415         /// <summary>
416         /// 最后一页
417         /// </summary>
418         /// <param name="sender"></param>
419         /// <param name="e"></param>
420         private void btLast_Click(object sender, EventArgs e)
421         {
422             _PageIndex = _PageCount;
423             LoadData();
424         }
425 
426         /// <summary>
427         /// 最后一页
428         /// </summary>
429         public void LastPage()
430         {
431             this._btLast.PerformClick();
432         }
433 
434         /// <summary>
435         /// combobox选择改变页号
436         /// </summary>
437         /// <param name="sender"></param>
438         /// <param name="e"></param>
439         private void comPageSize_SelectedIndexChanged(object sender, EventArgs e)
440         {
441             this._PageSize = Int32.Parse(_comPageSize.Text);
442             LoadData();
443         }
444 
445         /// <summary>
446         /// 翻到第几页
447         /// </summary>
448         /// <param name="sender"></param>
449         /// <param name="e"></param>
450         private void comToPageIndex_SelectedIndexChanged(object sender, EventArgs e)
451         {
452             this._PageIndex = Int32.Parse(_comToPageIndex.Text);
453             LoadData();
454         }
455     }
456 }
人生,总是有一些空城旧事,年华未央;总是有些季节,一季花凉,满地忧伤。许多事,看开了,便会峰回路转;许多梦,看淡了,便会云开日出。学会思索,学会珍藏,微笑领悟,默默坚强。
原文地址:https://www.cnblogs.com/yuzhou133/p/4134502.html