实例介绍
【实例简介】
如果您要处理用大量的比如说:一百个数据项(例如,收件箱里的邮件列表)的列表(译注:本文将items译为数据项),过滤、查找、分类,以及其它分组功能将很快变得困难而单调乏味。特别是排序和分组大大提高了一个列表中数据项的结构,默认情况下,这是我想要应用到我的所有列表的功能特性。特别情况下,我还会寻找一个允许将相似的项一起安排和分组列表/网格的控件,很像在Outlook 2003中使用的网格(或列表?)。 我知道这里有一些支持这类功能的商业列表/网格(控件);然而,在试用它们的时候我也遇到过若干bug。不能访问源代码(译注:因为商业目的)使得这非常令人沮丧,因此我想倒不如我写篇CodeProject文章,看看我是否可以拿出一个自定义解决方案。 因为网格比列表更加灵活,我决定实现一个可以一起分组的网格控件,正如Outlook一样。这个控件用C#2.0在VS 2005上实现的。现在,我不能保证这个实现没有bug,但至少它是免费的,并且它有源代码。因而,您可以根据您的需要修改它们,以适合于您自己的用途。无论如何要注意:这个控件并没有完成!一些功能也许不能正确地工作或完全不能工作。该控件主要关注于排序、分组并在网格上显示数据项,这些功能特性我想已做得相当地好了。
在网格中插入、更新和删除行及单元格不在我考虑的范围之内。
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | public class OutlookGridDefaultGroup : IOutlookGridGroup { /// <summary> /// 点击列的值 /// </summary> protected object val; /// <summary> /// 分组标题 /// </summary> protected string text; /// <summary> /// 是否折叠当前分组 /// </summary> protected bool collapsed; /// <summary> /// DataGridViewColumn列 /// </summary> protected DataGridViewColumn column; /// <summary> /// 当前分组包含的记录数目 /// </summary> protected int itemCount; /// <summary> /// 分组行高度 /// </summary> protected int height; /// <summary> /// OutlookgGridDefaultGroup对象 默认构造函数 /// </summary> public OutlookGridDefaultGroup() { val = null ; this .column = null ; height = 34; // default height } #region IOutlookGridGroup Members /// <summary> /// 分组对象显示的文本 /// </summary> public virtual string Text { get { if (column == null ) return string .Format( "Unbound group: {0} ({1})" , Value.ToString(), itemCount == 1 ? "1 item" : itemCount.ToString() " items" ); else return string .Format( "{0}: {1} ({2})" , column.HeaderText, Value.ToString(), itemCount == 1 ? "1 item" : itemCount.ToString() " items" ); } set { text = value; } } /// <summary> /// 值 /// </summary> public virtual object Value { get { return val; } set { val = value; } } /// <summary> /// 是否折叠 /// </summary> public virtual bool Collapsed { get { return collapsed; } set { collapsed = value; } } /// <summary> /// DataGridViewColumn列对象 /// </summary> public virtual DataGridViewColumn Column { get { return column; } set { column = value; } } /// <summary> /// 分组中包含的记录数目 /// </summary> public virtual int ItemCount { get { return itemCount; } set { itemCount = value; } } /// <summary> /// 分组行高度 /// </summary> public virtual int Height { get { return height; } set { height = value; } } #endregion #region ICloneable Members /// <summary> /// 实现ICloneable接口 /// </summary> /// <returns></returns> public virtual object Clone() { OutlookGridDefaultGroup gr = new OutlookGridDefaultGroup(); gr.column = this .column; gr.val = this .val; gr.collapsed = this .collapsed; gr.text = this .text; gr.height = this .height; return gr; } #endregion #region IComparable Members /// <summary> /// this is a basic string comparison operation. /// all items are grouped and categorised based on their string-appearance. /// </summary> /// <param name="obj">the value in the related column of the item to compare to</param> /// <returns></returns> public virtual int CompareTo( object obj) { return string .Compare(val.ToString(), obj.ToString()); } #endregion } #endregion OutlookGridDefaultGroup - implementation of the default grouping style #region OutlookGridAlphabeticGroup - an alphabet group implementation /// <summary> /// this group simple example of an implementation which groups the items into Alphabetic categories /// based only on the first letter of each item /// /// for this we need to override the Value property (used for comparison) /// and the CompareTo function. /// Also the Clone method must be overriden, so this Group object can create clones of itself. /// Cloning of the group is used by the OutlookGrid /// </summary> public class OutlookGridAlphabeticGroup : OutlookGridDefaultGroup { /// <summary> /// 按值首字母分组 /// </summary> public OutlookGridAlphabeticGroup() : base () { } /// <summary> /// 重写基类OutlookgGridDefaultGroup的Text属性 /// </summary> public override string Text { get { return string .Format( "Alphabetic: {1} ({2})" , column.HeaderText, Value.ToString(), itemCount == 1 ? "1 item" : itemCount.ToString() " items" ); } set { text = value; } } /// <summary> /// 重写基类OutlookgGridDefaultGroup的Value属性 /// </summary> public override object Value { get { return val; } set { val = value.ToString().Substring(0,1).ToUpper(); } } #region ICloneable Members /// <summary> /// each group class must implement the clone function /// </summary> /// <returns></returns> public override object Clone() { OutlookGridAlphabeticGroup gr = new OutlookGridAlphabeticGroup(); gr.column = this .column; gr.val = this .val; gr.collapsed = this .collapsed; gr.text = this .text; gr.height = this .height; return gr; } #endregion #region IComparable Members /// <summary> /// overide the CompareTo, so only the first character is compared, instead of the whole string /// this will result in classifying each item into a letter of the Alphabet. /// for instance, this is usefull when grouping names, they will be categorized under the letters A, B, C etc.. /// </summary> /// <param name="obj"></param> /// <returns></returns> public override int CompareTo( object obj) { return string .Compare(val.ToString(), obj.ToString().Substring(0, 1).ToUpper()); } #endregion IComparable Members } #endregion OutlookGridAlphabeticGroup - an alphabet group implementation #region 金额汇总分组 /// <summary> /// each arrange/grouping class must implement the IOutlookGridGroup interface /// the Group object will determine for each object in the grid, whether it /// falls in or outside its group. /// It uses the IComparable.CompareTo function to determine if the item is in the group. /// </summary> public class OutlookGridMoneyGroup : IOutlookGridGroup { /// <summary> /// 点击列的值 /// </summary> protected object val; /// <summary> /// 分组标题 /// </summary> protected string text; /// <summary> /// 是否折叠当前分组 /// </summary> protected bool collapsed; /// <summary> /// DataGridViewColumn列 /// </summary> protected DataGridViewColumn column; /// <summary> /// 当前分组包含的记录数目 /// </summary> protected int itemCount; /// <summary> /// 分组行高度 /// </summary> protected int height; /// <summary> /// 金额汇总列 /// </summary> protected int sumColumn = -1; /// <summary> /// 汇总金额数目 /// </summary> protected decimal total = 0.0m; /// <summary> /// 汇总金额 /// </summary> public decimal Total { get { return total; } set { total = value; } } /// <summary> /// 金额汇总列,默认没有-1 /// </summary> public int SumColumn { get { return sumColumn; } set { sumColumn = value; } } /// <summary> /// 金额分组对象 默认构造函数 /// </summary> public OutlookGridMoneyGroup() { val = null ; this .column = null ; height = 34; // default height } #region IOutlookGridGroup Members /// <summary> /// 分组对象显示的文本 /// </summary> public virtual string Text { get { if (column == null ) return string .Format( "未绑定组: {0} 记录数目: {1} (合计金额:{2})" , Value.ToString(), itemCount, total.ToString( "C" )); else return string .Format( "按{0}分组: {1} 记录数目: {2} (合计金额:{3})" , column.HeaderText, Value.ToString(), itemCount, total.ToString( "C" )); } set { text = value; } } /// <summary> /// 值 /// </summary> public virtual object Value { get { return val; } set { val = value; } } /// <summary> /// 是否折叠 /// </summary> public virtual bool Collapsed { get { return collapsed; } set { collapsed = value; } } /// <summary> /// DataGridViewColumn列对象 /// </summary> public virtual DataGridViewColumn Column { get { return column; } set { column = value; } } /// <summary> /// 分组中包含的记录数目 /// </summary> public virtual int ItemCount { get { return itemCount; } set { itemCount = value; } } /// <summary> /// 分组行高度 /// </summary> public virtual int Height { get { return height; } set { height = value; } } #endregion #region ICloneable Members /// <summary> /// 实现ICloneable接口 /// </summary> /// <returns></returns> public virtual object Clone() { OutlookGridMoneyGroup gr = new OutlookGridMoneyGroup(); gr.column = this .column; gr.val = this .val; gr.collapsed = this .collapsed; gr.text = this .text; gr.height = this .height; gr.sumColumn = this .sumColumn; gr.total = this .total; return gr; } #endregion #region IComparable Members /// <summary> /// this is a basic string comparison operation. /// all items are grouped and categorised based on their string-appearance. /// </summary> /// <param name="obj">the value in the related column of the item to compare to</param> /// <returns></returns> public virtual int CompareTo( object obj) { return string .Compare(val.ToString(), obj.ToString()); } #endregion } #endregion 金额汇总分组 #region 日期分组(带金额汇总功能) 继承OutlookGridMoneyGroup对象 /// <summary> /// each arrange/grouping class must implement the IOutlookGridGroup interface /// the Group object will determine for each object in the grid, whether it /// falls in or outside its group. /// It uses the IComparable.CompareTo function to determine if the item is in the group. /// </summary> public class OutlookGridDateGroup : OutlookGridMoneyGroup { /// <summary> /// 日期分组类型 /// </summary> protected DateGroupType groupType=DateGroupType.Day; /// <summary> /// 日期分组(带金额汇总功能) 继承OutlookGridMoneyGroup对象 /// </summary> public OutlookGridDateGroup() : base () { } /// <summary> /// 日期分组类型 /// </summary> public DateGroupType GroupType { get { return groupType; } set { groupType = value; } } /// <summary> /// 重写基类OutlookGridMoneyGroup的Text属性 /// </summary> public override string Text { get { //判断是否是绑定数据源 if ( base .column == null ) { //判断是否有金额列需要汇总 if ( base .SumColumn < 0) { return string .Format( "未绑定组: {0} 记录数目: {1} " , Value.ToString(), itemCount); } else { return string .Format( "未绑定组: {0} 记录数目: {1} (合计金额:{2})" , Value.ToString(), itemCount, total.ToString( "C" )); } } else { if ( base .SumColumn < 0) { return string .Format( "按{0}分组: {1} 记录数目: {2} " , column.HeaderText, val.ToString(), itemCount); } else { return string .Format( "按{0}分组: {1} 记录数目: {2} (合计金额:{3})" , column.HeaderText, val.ToString(), itemCount, total.ToString( "C" )); } } } set { text = value; } } /// <summary> /// 重写基类OutlookGridMoneyGroup的Value属性 /// </summary> public override object Value { get { return val; } set { val = GetFormatDate(value.ToString()); } } /// <summary> /// 得到格式化后的日期 /// </summary> /// <param name="date">日期</param> /// <returns></returns> private string GetFormatDate( string date) { string fomatValue; try { DateTime dt = DateTime.Parse(date); switch (groupType) { case DateGroupType.Day: fomatValue = dt.ToString( "yyyy-M-d" ); break ; case DateGroupType.Week: //显示中文日期名称 //dt.DayOfWeek.ToString(); fomatValue = dt.ToString( "dddd" , new System.Globalization.CultureInfo( "zh-CN" )); break ; case DateGroupType.Month: fomatValue = dt.ToString( "yyyy年M月" ); break ; case DateGroupType.Quarter: fomatValue = dt.Year "年第" ((dt.Month % 3 == 0) ? (dt.Month / 3) : (dt.Month / 3 1)) "季度" ; break ; case DateGroupType.Year: fomatValue = dt.Year "年度" ; break ; default : fomatValue = dt.ToString( "yyyy-M-d" ); break ; } } catch { fomatValue = date; } return fomatValue; } /// <summary> /// 获得中文星期名称 /// </summary> /// <param name="dt">日期对象</param> /// <returns></returns> public static string GetCnWeek(DateTime dt) { switch (dt.DayOfWeek) { case DayOfWeek.Monday: return "星期一" ; case DayOfWeek.Tuesday: return "星期二" ; case DayOfWeek.Wednesday: return "星期三" ; case DayOfWeek.Thursday: return "星期四" ; case DayOfWeek.Friday: return "星期五" ; case DayOfWeek.Saturday: return "星期六" ; case DayOfWeek.Sunday: return "星期日" ; default : return "" ; } } //在C#的WinForm开发中,使用DateTime.Now.DayOfWeek输出星期时,都是英文的, //如何输出中文呢?其实很简单,下面的的一个小小的函数即可实现: public static string WeekDayOfCN(DateTime dt) { string [] arrCnNames = new string [] { "日" , "一" , "二" , "三" , "四" , "五" , "六" }; return "星期" arrCnNames[( int )dt.DayOfWeek]; //System.DateTime.Now.GetDateTimeFormats('D')[2].Substring(0, 3).ToString(); } #region ICloneable Members /// <summary> /// 实现ICloneable接口 /// </summary> /// <returns></returns> public override object Clone() { OutlookGridDateGroup gr = new OutlookGridDateGroup(); gr.column = this .column; gr.val = this .val; gr.collapsed = this .collapsed; gr.text = this .text; gr.height = this .height; gr.sumColumn = this .sumColumn; gr.total = this .total; gr.groupType = this .groupType; return gr; } #endregion #region IComparable Members /// <summary> /// this is a basic string comparison operation. /// all items are grouped and categorised based on their string-appearance. /// </summary> /// <param name="obj">the value in the related column of the item to compare to</param> /// <returns></returns> public override int CompareTo( object obj) { return string .Compare(val.ToString(), GetFormatDate(obj.ToString())); } #endregion } #endregion 日期分组(带金额汇总功能) #region 日期分组类型 /// <summary> /// 日期分组类型 /// </summary> public enum DateGroupType { /// <summary> /// 按天分组 /// </summary> Day, /// <summary> /// 按星期分组 /// </summary> Week, /// <summary> /// 按月份分组 /// </summary> Month, /// <summary> /// 按季度分组 /// </summary> Quarter, /// <summary> /// 按年度分组 /// </summary> Year } #endregion 日期分组类型 |
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论