ucPagerEx.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10. namespace LYFZ.ManagementService.UCPager
  11. {
  12. /// <summary>
  13. /// 申明委托
  14. /// </summary>
  15. /// <param name="e"></param>
  16. /// <returns></returns>
  17. public delegate int EventPagingHandler(EventPagingArg e);
  18. public partial class ucPagerEx : UserControl
  19. {
  20. public ucPagerEx()
  21. {
  22. InitializeComponent();
  23. }
  24. public event EventPagingHandler EventPaging;
  25. /// <summary>
  26. /// 每页显示记录数
  27. /// </summary>
  28. private int _pageSize = 20;
  29. /// <summary>
  30. /// 每页显示记录数
  31. /// </summary>
  32. public int PageSize
  33. {
  34. get { return _pageSize; }
  35. set
  36. {
  37. _pageSize = value;
  38. GetPageCount();
  39. }
  40. }
  41. private int _nMax = 0;
  42. /// <summary>
  43. /// 总记录数
  44. /// </summary>
  45. public int NMax
  46. {
  47. get { return _nMax; }
  48. set
  49. {
  50. _nMax = value;
  51. GetPageCount();
  52. }
  53. }
  54. private int _pageCount = 0;
  55. /// <summary>
  56. /// 页数=总记录数/每页显示记录数
  57. /// </summary>
  58. public int PageCount
  59. {
  60. get { return _pageCount; }
  61. set { _pageCount = value; }
  62. }
  63. private int _pageCurrent = 0;
  64. /// <summary>
  65. /// 当前页号
  66. /// </summary>
  67. public int PageCurrent
  68. {
  69. get {
  70. if (_pageCurrent <= 0) {
  71. _pageCurrent = 1;
  72. }
  73. return _pageCurrent; }
  74. set { _pageCurrent = value; }
  75. }
  76. DataTable tbDataSource = null;
  77. /// <summary>
  78. /// 分页数据源
  79. /// </summary>
  80. public DataTable TbDataSource
  81. {
  82. get { return tbDataSource; }
  83. set { tbDataSource = value; }
  84. }
  85. private void GetPageCount()
  86. {
  87. if (this.NMax > 0)
  88. {
  89. this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.NMax) / Convert.ToDouble(this.PageSize)));
  90. }
  91. else
  92. {
  93. this.PageCount = 0;
  94. }
  95. }
  96. /// <summary>
  97. /// 重新刷新邦定
  98. /// </summary>
  99. public void RefreshBind()
  100. {
  101. this._pageCurrent = 1;
  102. this.Bind();
  103. }
  104. /// <summary>
  105. /// 翻页控件数据绑定的方法
  106. /// </summary>
  107. public void Bind()
  108. {
  109. if (this.EventPaging != null)
  110. {
  111. this.NMax = this.EventPaging(new EventPagingArg(this.PageCurrent));
  112. }
  113. if (this.PageCurrent <=0)
  114. {
  115. this.PageCurrent = 1;
  116. }
  117. if (this.PageCurrent > this.PageCount)
  118. {
  119. this.PageCurrent = this.PageCount;
  120. }
  121. if (this.PageCount == 1)
  122. {
  123. this.PageCurrent = 1;
  124. }
  125. lblPageCount.Text = this.PageCount.ToString();
  126. this.lblMaxPage.Text = "共" + this.NMax.ToString() + "条记录";
  127. this.txtCurrentPage.Text = this.PageCurrent.ToString();
  128. if (this.PageCurrent == 1)
  129. {
  130. this.btnPrev.Enabled = false;
  131. this.btnFirst.Enabled = false;
  132. }
  133. else
  134. {
  135. btnPrev.Enabled = true;
  136. btnFirst.Enabled = true;
  137. }
  138. if (this.PageCurrent == this.PageCount)
  139. {
  140. this.btnLast.Enabled = false;
  141. this.btnNext.Enabled = false;
  142. }
  143. else
  144. {
  145. btnLast.Enabled = true;
  146. btnNext.Enabled = true;
  147. }
  148. if (this.NMax == 0)
  149. {
  150. btnNext.Enabled = false;
  151. btnLast.Enabled = false;
  152. btnFirst.Enabled = false;
  153. btnPrev.Enabled = false;
  154. }
  155. DataTable dtble = TbDataSource;
  156. if (dtble != null)
  157. {
  158. this.lblthisPageCount.Text = dtble.Rows.Count.ToString();
  159. }
  160. }
  161. private void btnFirst_Click(object sender, EventArgs e)
  162. {
  163. PageCurrent = 1;
  164. this.Bind();
  165. }
  166. private void btnPrev_Click(object sender, EventArgs e)
  167. {
  168. PageCurrent -= 1;
  169. if (PageCurrent <= 0)
  170. {
  171. PageCurrent = 1;
  172. }
  173. this.Bind();
  174. }
  175. private void btnNext_Click(object sender, EventArgs e)
  176. {
  177. this.PageCurrent += 1;
  178. if (PageCurrent > PageCount)
  179. {
  180. PageCurrent = PageCount;
  181. }
  182. this.Bind();
  183. }
  184. private void btnLast_Click(object sender, EventArgs e)
  185. {
  186. PageCurrent = PageCount;
  187. this.Bind();
  188. }
  189. private void btnGo_Click(object sender, EventArgs e)
  190. {
  191. if (this.txtCurrentPage.Text != null && txtCurrentPage.Text != "")
  192. {
  193. if (Int32.TryParse(txtCurrentPage.Text, out _pageCurrent))
  194. {
  195. this.Bind();
  196. }
  197. else
  198. {
  199. MessageBoxCustom.Show("输入数字格式错误!");
  200. }
  201. }
  202. }
  203. private void txtCurrentPage_KeyDown(object sender, KeyEventArgs e)
  204. {
  205. if (e.KeyCode == Keys.Enter)
  206. {
  207. this.Bind();
  208. }
  209. }
  210. private void lblbtnShowAll_Click(object sender, EventArgs e)
  211. {
  212. if (this.NMax > 10000)
  213. {
  214. if (MessageBoxCustom.Show("超过万条数据加载全部会影响软件性能,是否要显示全部数据?", msgBoxButton: MessageBoxButtons.YesNo) == DialogResult.Yes)
  215. {
  216. this.PageSize = this.NMax;
  217. this.PageCurrent = 1;
  218. this.Bind();
  219. }
  220. }
  221. else
  222. {
  223. this.PageSize = this.NMax;
  224. this.PageCurrent = 1;
  225. this.Bind();
  226. }
  227. }
  228. }
  229. /// <summary>
  230. /// 自定义事件数据基类
  231. /// </summary>
  232. public class EventPagingArg : EventArgs
  233. {
  234. private int _intPageIndex;
  235. public EventPagingArg(int PageIndex)
  236. {
  237. _intPageIndex = PageIndex;
  238. }
  239. }
  240. /// <summary>
  241. /// 数据源提供
  242. /// </summary>
  243. public class PageData
  244. {
  245. private int _PageSize = 10;
  246. private int _PageIndex = 1;
  247. private int _PageCount = 0;
  248. private int _TotalCount = 0;
  249. private string _TableName;//表名
  250. private string _QueryFieldName = "*";//表字段FieldStr
  251. private string _OrderStr = string.Empty; //排序_SortStr
  252. private string _QueryCondition = string.Empty;//查询的条件 RowFilter
  253. private string _PrimaryKey = string.Empty;//主键
  254. /// <summary>
  255. /// 显示页数
  256. /// </summary>
  257. public int PageSize
  258. {
  259. get
  260. {
  261. return _PageSize;
  262. }
  263. set
  264. {
  265. _PageSize = value;
  266. }
  267. }
  268. /// <summary>
  269. /// 当前页
  270. /// </summary>
  271. public int PageIndex
  272. {
  273. get
  274. {
  275. return _PageIndex;
  276. }
  277. set
  278. {
  279. _PageIndex = value;
  280. }
  281. }
  282. /// <summary>
  283. /// 总页数
  284. /// </summary>
  285. public int PageCount
  286. {
  287. get
  288. {
  289. return _PageCount;
  290. }
  291. }
  292. /// <summary>
  293. /// 总记录数
  294. /// </summary>
  295. public int TotalCount
  296. {
  297. get
  298. {
  299. return _TotalCount;
  300. }
  301. }
  302. /// <summary>
  303. /// 表名,包括视图
  304. /// </summary>
  305. public string TableName
  306. {
  307. get
  308. {
  309. return _TableName;
  310. }
  311. set
  312. {
  313. _TableName = value;
  314. }
  315. }
  316. /// <summary>
  317. /// 表字段FieldStr
  318. /// </summary>
  319. public string QueryFieldName
  320. {
  321. get
  322. {
  323. return _QueryFieldName;
  324. }
  325. set
  326. {
  327. _QueryFieldName = value;
  328. }
  329. }
  330. /// <summary>
  331. /// 排序字段
  332. /// </summary>
  333. public string OrderStr
  334. {
  335. get
  336. {
  337. return _OrderStr;
  338. }
  339. set
  340. {
  341. _OrderStr = value;
  342. }
  343. }
  344. /// <summary>
  345. /// 查询条件
  346. /// </summary>
  347. public string QueryCondition
  348. {
  349. get
  350. {
  351. return _QueryCondition;
  352. }
  353. set
  354. {
  355. _QueryCondition = value;
  356. }
  357. }
  358. /// <summary>
  359. /// 主键
  360. /// </summary>
  361. public string PrimaryKey
  362. {
  363. get
  364. {
  365. return _PrimaryKey;
  366. }
  367. set
  368. {
  369. _PrimaryKey = value;
  370. }
  371. }
  372. public DataSet QueryDataTable()
  373. {
  374. //SqlParameter[] parameters = {
  375. // new SqlParameter("@Tables", SqlDbType.VarChar, 255),
  376. // new SqlParameter("@PrimaryKey" , SqlDbType.VarChar , 255),
  377. // new SqlParameter("@Sort", SqlDbType.VarChar , 255 ),
  378. // new SqlParameter("@CurrentPage", SqlDbType.Int),
  379. // new SqlParameter("@PageSize", SqlDbType.Int),
  380. // new SqlParameter("@Fields", SqlDbType.VarChar, 255),
  381. // new SqlParameter("@Filter", SqlDbType.VarChar,1000),
  382. // new SqlParameter("@Group" ,SqlDbType.VarChar , 1000 )
  383. // };
  384. //@TableName = N'dbo.tb_Customer',
  385. // @ReFieldsStr = N'*',
  386. // @OrderString = N'ShopName desc',
  387. // @WhereString = N'1=1',
  388. // @PageSize = 200,
  389. // @PageIndex = 10,
  390. // @TotalRecord = @TotalRecord OUTPUT
  391. SqlParameter[] parameters = {
  392. new SqlParameter("@TableName", SqlDbType.VarChar , 2000),
  393. new SqlParameter("@ReFieldsStr" , SqlDbType.VarChar , 2000),
  394. new SqlParameter("@OrderString", SqlDbType.VarChar , 2000 ),
  395. new SqlParameter("@WhereString", SqlDbType.VarChar, 2000),
  396. new SqlParameter("@PageSize", SqlDbType.Int),
  397. new SqlParameter("@PageIndex", SqlDbType.Int),
  398. new SqlParameter("@TotalRecord", SqlDbType.Int)
  399. };
  400. parameters[0].Value =_TableName ;
  401. parameters[1].Value = _QueryFieldName;
  402. parameters[2].Value = _OrderStr;
  403. parameters[3].Value = _QueryCondition;
  404. parameters[4].Value = PageSize;
  405. parameters[5].Value = PageIndex;
  406. parameters[6].Direction = ParameterDirection.Output;
  407. DataSet ds = LYFZ.Helper.SQLHelper.RunProcedure("PROCE_SQL2005PAGECHANGE", parameters, _TableName + "_ds");
  408. int tempRecord = Convert.ToInt32(parameters[6].Value);
  409. _TotalCount = tempRecord;//GetTotalCount();
  410. if (_TotalCount == 0)
  411. {
  412. _PageIndex = 0;
  413. _PageCount = 0;
  414. }
  415. else
  416. {
  417. _PageCount = _TotalCount % _PageSize == 0 ? _TotalCount / _PageSize : _TotalCount / _PageSize + 1;
  418. if (_PageIndex > _PageCount)
  419. {
  420. _PageIndex = _PageCount;
  421. parameters[4].Value = _PageSize;
  422. ds = QueryDataTable();
  423. }
  424. }
  425. return ds;
  426. }
  427. /// <summary>
  428. /// 获取总记录数
  429. /// </summary>
  430. /// <returns></returns>
  431. public int GetTotalCount()
  432. {
  433. string strSql = " select count(1) from " + _TableName;
  434. if (_QueryCondition != string.Empty)
  435. {
  436. strSql += " where " + _QueryCondition;
  437. }
  438. return int.Parse(LYFZ.Helper.SQLHelper.GetSingle(strSql).ToString());
  439. }
  440. }
  441. }