有天在想工作上的事的时候,看着.net原有的DataGridView,想起以前我写过的一篇文章,总结了一个好的Gird控件应该具备哪些功能,在那里我提及到了分组功能,就像jqGrid那样,

  其实这样的显示型式很常见,就在平时邮箱的邮件列表就是按这种分组型式显示的,按今天、昨天、上周之类,在购物网站的历史订单处也可以看见这种Grid表的身影。但是原有的DataGridView并不支持这种分组功能。那只能扩展一下了。

  之前写了一个多维表头的GirdView,有经验了,知道搞这种图形化的东西无非都是用GDI+,上网找了一些文章后有点愣住了,之前画表头的是在DataGridView的OnPaint方法里把表头描绘出来,但是这里画分组的话就不同了,不是在DataGridViewRow的Paint方法里面处理。

  因此要完成这个可分组DataGridView需要对两个类进行拓展,一个是DataGridView,另一个是DataGirdViewRow。而实现这个分组DataGridView的效果大体步骤就是先把分组的标题行添加到GirdView里面,然后逐个把该组的数据行添加到GridView里面并控制它的显示状态,在分组的标题行进行相关的操作来改变数据行显示状态达到分组显示的效果。

下面则逐个类来介绍吧!

  GroupGridView继承DataGridView,以下是它的一些字段和属性的定义

成员名称

数据类型

修饰符

描述

GroupFieldName

String

public

要分组的字段的名称,只能是类的属性名或者是DataTable的列名

objDataSource

Object

Protected

使用分组时暂时记录数据源

GroupRowTemplate

GroupGridViewRow

Public

分组行实例的模板

isGroupping

Bool

Private

是否使用分组显示

  如果要使用这个GroupGridView的话,还要默认地对原本的DataGridView进行一些设置,这些设置我都塞到了构造函数里面,大体上分三类,分别是操作设置,样式设置和部分属性或字段的赋值,代码如下

  1. public GroupGridView()
  2. {
  3. //对GridView的操作的设置
  4. this.AllowUserToAddRows = false;
  5. this.AllowUserToDeleteRows = false;
  6. this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  7.  
  8. //对GridView的样式设置
  9. this.EditMode = DataGridViewEditMode.EditProgrammatically;
  10. this.RowHeadersVisible = false;
  11. this.CellBorderStyle = DataGridViewCellBorderStyle.RaisedHorizontal;
  12.  
  13. //对实例的部分成员赋值
  14. isGroupping = false;
  15. GroupRowTemplate = new GroupGridViewRow();
  16. }

  其实GroupGridView要做的事就是能增加分组,在单击和双击分组行时作处理,还有就是数据绑定时把数据分组添加到GirdView里。那么就逐个方法来介绍

第一个是增加分组的

  1. public GroupGridViewRow CreateGroupGridViewRow(string title)
  2. {
  3. GroupGridViewRow group = this.GroupRowTemplate.Clone() as GroupGridViewRow;
  4. if (group == null)
  5. throw new NullReferenceException("组模板为空或者组模板类型不是GroupGridViewRow");
  6. group.Title = title;
  7. group.ParentGridView = this;
  8. group.CreateCells(this, group.Title);
  9. this.Rows.Add(group);
  10. group.CollapseGroup();
  11. return group;
  12. }

主要是按照模板拷贝一个GroupGridViewRow的实例,设置相应的属性后就把它添加到GirdView里面。

然后到鼠标对分组行的点击事件,单击展开/折叠图标就展示或隐藏该组的数据。双击分组行同样也达到这种显示状态切换的效果。

  1. protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
  2. {
  3. if (e.RowIndex == - ||e.ColumnIndex==-|| e.Button != System.Windows.Forms.MouseButtons.Left || e.Clicks != )
  4. return;
  5. GroupGridViewRow group = this.Rows[e.RowIndex] as GroupGridViewRow;
  6. if (group != null && group.IsIconHit(e))
  7. group.Toggle();
  8. base.OnCellMouseDown(e);
  9. }
  10.  
  11. protected override void OnCellDoubleClick(DataGridViewCellEventArgs e)
  12. {
  13. if (e.RowIndex == -||e.ColumnIndex==-)
  14. return;
  15. GroupGridViewRow group = this.Rows[e.RowIndex] as GroupGridViewRow;
  16. if (group != null )
  17. group.Toggle();
  18. base.OnCellDoubleClick(e);
  19. }

Toggle和IsIconHit方法在GroupGridViewRow里会定义,在介绍GroupGridViewRow会列出该方法的定义,在单击时就要通过IsIconHit判断鼠标是否点击到了图片,当然是点击中图标才会经行状态切换,而双击则不需要了。无论是双击和单击都要判断当前鼠标所在的行是分组行,如果是数据行的话就不会作任何操作了。

为了方便点绑定到数据,我重新定义了控件的DataSource属性。当分组字段信息(GroupFieldName属性)不存在时就会使用GridView默认的绑定数据,如果存在就会分组筛选出数据,然后逐个组去把行添加进去。

  1. public new object DataSource
  2. {
  3. get
  4. {
  5. if (isGroupping) return objDataSource;
  6. return base.DataSource;
  7. }
  8. set
  9. {
  10. if (string.IsNullOrEmpty(GroupFieldName)
  11. || string.IsNullOrWhiteSpace(GroupFieldName))
  12. {
  13.  
  14. foreach (DataGridViewColumn col in this.Columns)
  15. col.SortMode = DataGridViewColumnSortMode.Automatic;
  16. base.DataSource = value;
  17. isGroupping = false;
  18. }
  19. else
  20. {
  21.  
  22. foreach (DataGridViewColumn col in this.Columns)
  23. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  24. if (value is IEnumerable)
  25. BindIEnumerableDataSource(value as IEnumerable);
  26. else if (value is DataTable)
  27. BindDataTableDataSouce(value as DataTable);
  28. else if (value is DataSet)
  29. BindDataSetDataSource(value as DataSet);
  30. else
  31. {
  32. throw new NotImplementedException("不支持此类型作数据源");
  33. }
  34. objDataSource = value;
  35. isGroupping = true;
  36.  
  37. }
  38. }
  39. }

现在能自动绑定的数据源只能是可枚举的或DataTable,DataSet其实是把里面第一个DataTable作为数据源而已。不同类型的数据有相应的处理方法

  1. private void BindIEnumerableDataSource(IEnumerable enumerable)
  2. {
  3. IEnumerable iends = enumerable;
  4. if (iends == null) return;
  5. Type dsItemType = null;
  6. foreach (object item in iends)
  7. dsItemType = item.GetType();
  8. if (iends == null) return;
  9.  
  10. PropertyInfo proInfo = dsItemType.GetProperty(GroupFieldName);
  11.  
  12. Dictionary<string, List<object>> groupDataSource = new Dictionary<string, List<object>>();
  13. foreach (object item in iends)
  14. {
  15. string tempStr = proInfo.GetValue(item, null).ToString();
  16. if (!groupDataSource.ContainsKey(tempStr))
  17. groupDataSource[tempStr] = new List<object>();
  18. groupDataSource[tempStr].Add(item);
  19. }
  20.  
  21. List<string> colFildNames = new List<string>(this.Columns.Count);
  22. foreach (DataGridViewColumn col in this.Columns)
  23. colFildNames.Add(col.DataPropertyName);
  24. GroupGridViewRow group = null;
  25. List<object> datas = new List<object>(colFildNames.Count);
  26. foreach (KeyValuePair<string, List<object>> gi in groupDataSource)
  27. {
  28. group = CreateGroupGridViewRow(gi.Key);
  29. foreach (object celli in gi.Value)
  30. {
  31. foreach (string colName in colFildNames)
  32. {
  33. datas.Add(dsItemType.GetProperty(colName).GetValue(celli, null));
  34. }
  35. group.AddRowToGroup(datas.ToArray());
  36. datas.Clear();
  37. }
  38. group.CollapseGroup();
  39. }
  40. }

对于可枚举的数据源,我就通过反射把相应的属性的值都拿出来填到DataGridViewRow里面。

  1. private void BindDataTableDataSouce(DataTable table)
  2. {
  3. Dictionary<string, List<DataRow>> groupDataSource = new Dictionary<string, List<DataRow>>();
  4. foreach (DataRow row in table.Rows)
  5. {
  6. string tempStr = row[GroupFieldName].ToString();
  7. if (!groupDataSource.ContainsKey(tempStr))
  8. groupDataSource[tempStr] = new List<DataRow>();
  9. groupDataSource[tempStr].Add(row);
  10. }
  11.  
  12. List<string> colFildNames = new List<string>(this.Columns.Count);
  13. foreach (DataGridViewColumn col in this.Columns)
  14. colFildNames.Add(col.DataPropertyName);
  15. GroupGridViewRow group = null;
  16. List<object> datas = new List<object>(colFildNames.Count);
  17. foreach (KeyValuePair<string, List<DataRow>> gi in groupDataSource)
  18. {
  19. group = CreateGroupGridViewRow(gi.Key);
  20. foreach (DataRow celli in gi.Value)
  21. {
  22. foreach (string colName in colFildNames)
  23. {
  24. datas.Add(celli[colName]);
  25. }
  26. group.AddRowToGroup(datas.ToArray());
  27. datas.Clear();
  28. }
  29. group.CollapseGroup();
  30. }
  31. }

DataTable的绑定也类似,更方便的是DataTable不需要用反射了,直接通过行列的访问就可以了。

GruopGridView介绍就到此结束,下面则介绍另一个类GroupGridViewRow,它是继承DataGridViewRow。而GDI+的描绘都是在这个方法里面。也是先看看里面的成员

成员名称

数据类型

修饰符

描述

IsExpanded

bool

public

分组的状态,是展开还是折叠

Title

string

public

分组的标题,通常是分组的数据

ParentGridView

GroupGridView

public

分组行所在的GridView

groupRows

List<DataGridViewRow>

private

此分组包含的数据行

  那么一个GroupGridViewRow的要处理的事就有以下几个,对分组下的数据行的状态控制,判断鼠标单击是否命中图标,增加一行数据到该分组下,还有最重要的就是描绘出分组行的外观。

先列举数据行的状态控制方法,ExpandGroup()展开分组,CollapseGroup()折叠分组,还有Toggle()切换

  1. public void ExpandGroup()
  2. {
  3. IsExpanded = true;
  4. foreach (DataGridViewRow row in groupRows)
  5. row.Visible = true;
  6. }
  7.  
  8. public void CollapseGroup()
  9. {
  10. IsExpanded = false;
  11. foreach (DataGridViewRow row in groupRows)
  12. row.Visible = false;
  13. }
  14.  
  15. public void Toggle()
  16. {
  17. if (IsExpanded)
  18. CollapseGroup();
  19. else
  20. ExpandGroup();
  21. }

实际上就是通过遍历groupRows集合,改变其显示状态而已。

判断单击图标的方法如下

  1. public bool IsIconHit(DataGridViewCellMouseEventArgs e)
  2. {
  3. Rectangle groupBound = this.ParentGridView.GetRowDisplayRectangle(e.RowIndex, false);
  4.  
  5. if (
  6. e.X > groupBound.Left + &&
  7. e.X < groupBound.Left + &&
  8. e.Y > ((groupBound.Height - ) - ) / - &&
  9. e.Y < ((groupBound.Height - ) - ) / + -
  10. )
  11. return true;
  12.  
  13. return false;
  14. }

主要是通过鼠标指针当前所在的坐标是不是在图标的范围以内,那个范围有点不好把握,要通过DataGridView的GetRowDisplayRectangle方法得到分组行的矩形,Left,X这两个属性有点分不清了。

增加数据行的方法如下

  1. public DataGridViewRow AddRowToGroup(params object[] values)
  2. {
  3. DataGridViewRow row = this.ParentGridView.RowTemplate.Clone() as DataGridViewRow;
  4. if (row == null) throw new NullReferenceException("行模板为空或者组模板类型不是DataGridViewRow");
  5.  
  6. row.CreateCells(this.ParentGridView, values);
  7. this.ParentGridView.Rows.Add(row);
  8. this.groupRows.Add(row);
  9. return row;
  10. }

也复杂,通过DataGridView的普通数据行(不是分组行)的目标拷贝,填上数据,然后分别添加到这个分组的数据行集合中和GridView中。

最后到描绘分组行的方法,重写Paint方法,这个基本上是参考园友老虎哥的代码的,也作了一些小的调整,解决了水平滚动之后文字和图标有重影的问题,所以老虎哥看见了不要怪哈!

  1. protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle rowBounds, int rowIndex, DataGridViewElementStates rowState, bool isFirstDisplayedRow, bool isLastVisibleRow)
  2. {
  3.  
  4. int holdWidth = this.ParentGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);
  5. Color backgroudColor;
  6. if (this.Selected)
  7. backgroudColor = this.ParentGridView.DefaultCellStyle.SelectionBackColor;
  8. else
  9. backgroudColor = this.ParentGridView.DefaultCellStyle.BackColor;
  10. using (Brush backgroudBrush = new SolidBrush(backgroudColor))
  11. {
  12. graphics.FillRectangle(backgroudBrush, rowBounds.X,rowBounds.Y,holdWidth,rowBounds.Height);
  13. }
  14. using (Brush bottomLineBrush=new SolidBrush(Color.FromKnownColor( KnownColor.GradientActiveCaption)))
  15. {
  16. graphics.FillRectangle(bottomLineBrush, rowBounds.Left, rowBounds.Top + rowBounds.Height-, holdWidth, );
  17. }
  18.  
  19. StringFormat sf = new StringFormat();
  20. sf.LineAlignment = StringAlignment.Center;
  21. Font font = new Font(this.ParentGridView.Font, FontStyle.Bold);
  22. graphics.DrawString(this.Title, font, Brushes.Black,
  23. rowBounds.Left - this.ParentGridView.HorizontalScrollingOffset + , rowBounds.Top + rowBounds.Height / , sf);
  24.  
  25. int symbolX = rowBounds.Left - this.ParentGridView.HorizontalScrollingOffset + ;
  26. int symbolY =rowBounds.Y+ ((rowBounds.Height - ) - ) / -;
  27. if (Application.RenderWithVisualStyles)
  28. {
  29.  
  30. VisualStyleRenderer glyphRenderer;
  31. if (this.IsExpanded)
  32. glyphRenderer = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
  33. else
  34. glyphRenderer = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Closed);
  35.  
  36. Rectangle glyphRectangle =new Rectangle(symbolX, symbolY, , );
  37.  
  38. glyphRenderer.DrawBackground(graphics, glyphRectangle);
  39.  
  40. }
  41. else
  42. {
  43. int h = ;
  44. int w = ;
  45. int x = symbolX;
  46. int y = symbolY;
  47. graphics.DrawRectangle(new Pen(SystemBrushes.ControlDark), x, y, w, h);
  48. graphics.FillRectangle(new SolidBrush(Color.White),
  49. x + , y + , w - , h - );
  50.  
  51. //画横线
  52. graphics.DrawLine(new Pen(new SolidBrush(Color.Black)),
  53. x + , y + , x + w - , y + );
  54.  
  55. //画竖线
  56. if (!this.IsExpanded)
  57. graphics.DrawLine(new Pen(new SolidBrush(Color.Black)),
  58. x + , y + , x + , y + h - );
  59. }
  60. }

我自己写出来的话还是有点棘手的,绘制对于我自己而言就分了三部分,分组栏的背景边线描绘,文字的填写,还有图标的描绘,而图标在这里很好的考虑那个显示效果,分了渐变效果和单色效果。我也该多学习学习。

最后列一下控件的使用,列的设置分组设置,数据绑定如下,CreateColumn是我定义的方法,主要是构造一个新的列,对其进行设置之后就添加到GridView里面。

  1. groupGridView21.GroupFieldName = "name";
  2.  
  3. CreateColumn("id", "id", groupGridView21);
  4. CreateColumn("invdate", "invdate", groupGridView21);
  5. CreateColumn("note", "note", groupGridView21);
  6. CreateColumn("amount", "amount", groupGridView21);
  7. CreateColumn("tax", "tax", groupGridView21);
  8. CreateColumn("total", "total", groupGridView21);
  9. CreateColumn("name", "name", groupGridView21);
  10.  
  11. groupGridView21.DataSource = datasource;

效果就这样,数据我完全拿了那个jqGrid的Demo里面的数据

控件的缺点还是跟老虎哥的控件类似,要把显示的列逐个添加,不支持自动添加列,并且分组显示的时候不能对列经行排序。最后附上控件的完整源码,介绍完毕,谢谢!

  1. public class GroupGridView:DataGridView
  2. {
  3. public string GroupFieldName { get; set; }
  4.  
  5. protected object objDataSource;
  6.  
  7. public GroupGridViewRow GroupRowTemplate { get;protected set; }
  8.  
  9. private bool isGroupping;
  10.  
  11. public GroupGridView()
  12. {
  13. //对GridView的操作的设置
  14. this.AllowUserToAddRows = false;
  15. this.AllowUserToDeleteRows = false;
  16. this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  17.  
  18. //对GridView的样式设置
  19. this.EditMode = DataGridViewEditMode.EditProgrammatically;
  20. this.RowHeadersVisible = false;
  21. this.CellBorderStyle = DataGridViewCellBorderStyle.RaisedHorizontal;
  22.  
  23. //对实例的部分成员赋值
  24. isGroupping = false;
  25. GroupRowTemplate = new GroupGridViewRow();
  26. }
  27.  
  28. public GroupGridViewRow CreateGroupGridViewRow(string title)
  29. {
  30. GroupGridViewRow group = this.GroupRowTemplate.Clone() as GroupGridViewRow;
  31. if (group == null)
  32. throw new NullReferenceException("组模板为空或者组模板类型不是GroupGridViewRow");
  33. group.Title = title;
  34. group.ParentGridView = this;
  35. group.CreateCells(this, group.Title);
  36. this.Rows.Add(group);
  37. //do
  38. //{
  39. // group.Toggle();
  40. //} while (group.IsExpanded);
  41. group.CollapseGroup();
  42. return group;
  43. }
  44.  
  45. public new object DataSource
  46. {
  47. get
  48. {
  49. if (isGroupping) return objDataSource;
  50. return base.DataSource;
  51. }
  52. set
  53. {
  54. if (string.IsNullOrEmpty(GroupFieldName)
  55. || string.IsNullOrWhiteSpace(GroupFieldName))
  56. {
  57.  
  58. foreach (DataGridViewColumn col in this.Columns)
  59. col.SortMode = DataGridViewColumnSortMode.Automatic;
  60. base.DataSource = value;
  61. isGroupping = false;
  62. }
  63. else
  64. {
  65.  
  66. foreach (DataGridViewColumn col in this.Columns)
  67. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  68. if (value is IEnumerable)
  69. BindIEnumerableDataSource(value as IEnumerable);
  70. else if (value is DataTable)
  71. BindDataTableDataSouce(value as DataTable);
  72. else if (value is DataSet)
  73. BindDataSetDataSource(value as DataSet);
  74. else
  75. {
  76. throw new NotImplementedException("不支持此类型作数据源");
  77. }
  78. objDataSource = value;
  79. isGroupping = true;
  80.  
  81. }
  82. }
  83. }
  84.  
  85. protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
  86. {
  87. if (e.RowIndex == - ||e.ColumnIndex==-|| e.Button != System.Windows.Forms.MouseButtons.Left || e.Clicks != )
  88. return;
  89. GroupGridViewRow group = this.Rows[e.RowIndex] as GroupGridViewRow;
  90. if (group != null && group.IsIconHit(e))
  91. group.Toggle();
  92. base.OnCellMouseDown(e);
  93. }
  94.  
  95. protected override void OnCellDoubleClick(DataGridViewCellEventArgs e)
  96. {
  97. if (e.RowIndex == -||e.ColumnIndex==-)
  98. return;
  99. GroupGridViewRow group = this.Rows[e.RowIndex] as GroupGridViewRow;
  100. if (group != null )
  101. group.Toggle();
  102. base.OnCellDoubleClick(e);
  103. }
  104.  
  105. private void BindIEnumerableDataSource(IEnumerable enumerable)
  106. {
  107. IEnumerable iends = enumerable;
  108. if (iends == null) return;
  109. Type dsItemType = null;
  110. foreach (object item in iends)
  111. dsItemType = item.GetType();
  112. if (iends == null) return;
  113.  
  114. PropertyInfo proInfo = dsItemType.GetProperty(GroupFieldName);
  115.  
  116. Dictionary<string, List<object>> groupDataSource = new Dictionary<string, List<object>>();
  117. foreach (object item in iends)
  118. {
  119. string tempStr = proInfo.GetValue(item, null).ToString();
  120. if (!groupDataSource.ContainsKey(tempStr))
  121. groupDataSource[tempStr] = new List<object>();
  122. groupDataSource[tempStr].Add(item);
  123. }
  124.  
  125. List<string> colFildNames = new List<string>(this.Columns.Count);
  126. foreach (DataGridViewColumn col in this.Columns)
  127. colFildNames.Add(col.DataPropertyName);
  128. GroupGridViewRow group = null;
  129. List<object> datas = new List<object>(colFildNames.Count);
  130. foreach (KeyValuePair<string, List<object>> gi in groupDataSource)
  131. {
  132. group = CreateGroupGridViewRow(gi.Key);
  133. foreach (object celli in gi.Value)
  134. {
  135. foreach (string colName in colFildNames)
  136. {
  137. datas.Add(dsItemType.GetProperty(colName).GetValue(celli, null));
  138. }
  139. group.AddRowToGroup(datas.ToArray());
  140. datas.Clear();
  141. }
  142. //do
  143. //{
  144. // group.Toggle();
  145. //} while (group.IsExpanded);
  146. group.CollapseGroup();
  147. }
  148. }
  149.  
  150. private void BindDataTableDataSouce(DataTable table)
  151. {
  152. Dictionary<string, List<DataRow>> groupDataSource = new Dictionary<string, List<DataRow>>();
  153. foreach (DataRow row in table.Rows)
  154. {
  155. string tempStr = row[GroupFieldName].ToString();
  156. if (!groupDataSource.ContainsKey(tempStr))
  157. groupDataSource[tempStr] = new List<DataRow>();
  158. groupDataSource[tempStr].Add(row);
  159. }
  160.  
  161. List<string> colFildNames = new List<string>(this.Columns.Count);
  162. foreach (DataGridViewColumn col in this.Columns)
  163. colFildNames.Add(col.DataPropertyName);
  164. GroupGridViewRow group = null;
  165. List<object> datas = new List<object>(colFildNames.Count);
  166. foreach (KeyValuePair<string, List<DataRow>> gi in groupDataSource)
  167. {
  168. group = CreateGroupGridViewRow(gi.Key);
  169. foreach (DataRow celli in gi.Value)
  170. {
  171. foreach (string colName in colFildNames)
  172. {
  173. datas.Add(celli[colName]);
  174. }
  175. group.AddRowToGroup(datas.ToArray());
  176. datas.Clear();
  177. }
  178. //do
  179. //{
  180. // group.Toggle();
  181. //} while (group.IsExpanded);
  182. group.CollapseGroup();
  183. }
  184. }
  185.  
  186. private void BindDataSetDataSource(DataSet dataset)
  187. {
  188. if (dataset == null || dataset.Tables.Count == null) return;
  189. BindDataTableDataSouce(dataset.Tables[]);
  190. }
  191. }
  192.  
  193. public class GroupGridViewRow:DataGridViewRow
  194. {
  195. public bool IsExpanded { get;private set; }
  196.  
  197. public string Title { get; set; }
  198.  
  199. public GroupGridView ParentGridView { get; set; }
  200.  
  201. private List<DataGridViewRow> groupRows { get; set; }
  202.  
  203. public GroupGridViewRow()
  204. {
  205. IsExpanded = false;
  206. groupRows = new List<DataGridViewRow>();
  207. }
  208.  
  209. public void ExpandGroup()
  210. {
  211. IsExpanded = true;
  212. foreach (DataGridViewRow row in groupRows)
  213. row.Visible = true;
  214. }
  215.  
  216. public void CollapseGroup()
  217. {
  218. IsExpanded = false;
  219. foreach (DataGridViewRow row in groupRows)
  220. row.Visible = false;
  221. }
  222.  
  223. public void Toggle()
  224. {
  225. if (IsExpanded)
  226. CollapseGroup();
  227. else
  228. ExpandGroup();
  229. }
  230.  
  231. public bool IsIconHit(DataGridViewCellMouseEventArgs e)
  232. {
  233. Rectangle groupBound = this.ParentGridView.GetRowDisplayRectangle(e.RowIndex, false);
  234.  
  235. if (
  236. e.X > groupBound.Left + &&
  237. e.X < groupBound.Left + &&
  238. e.Y > ((groupBound.Height - ) - ) / - &&
  239. e.Y < ((groupBound.Height - ) - ) / + -
  240. )
  241. return true;
  242.  
  243. return false;
  244. }
  245.  
  246. public DataGridViewRow AddRowToGroup(params object[] values)
  247. {
  248. DataGridViewRow row = this.ParentGridView.RowTemplate.Clone() as DataGridViewRow;
  249. if (row == null) throw new NullReferenceException("行模板为空或者组模板类型不是DataGridViewRow");
  250.  
  251. row.CreateCells(this.ParentGridView, values);
  252. this.ParentGridView.Rows.Add(row);
  253. this.groupRows.Add(row);
  254. return row;
  255. }
  256.  
  257. protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle rowBounds, int rowIndex, DataGridViewElementStates rowState, bool isFirstDisplayedRow, bool isLastVisibleRow)
  258. {
  259.  
  260. int holdWidth = this.ParentGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);
  261. Color backgroudColor;
  262. if (this.Selected)
  263. backgroudColor = this.ParentGridView.DefaultCellStyle.SelectionBackColor;
  264. else
  265. backgroudColor = this.ParentGridView.DefaultCellStyle.BackColor;
  266. using (Brush backgroudBrush = new SolidBrush(backgroudColor))
  267. {
  268. graphics.FillRectangle(backgroudBrush, rowBounds.X,rowBounds.Y,holdWidth,rowBounds.Height);
  269. }
  270. using (Brush bottomLineBrush=new SolidBrush(Color.FromKnownColor( KnownColor.GradientActiveCaption)))
  271. {
  272. graphics.FillRectangle(bottomLineBrush, rowBounds.Left, rowBounds.Top + rowBounds.Height-, holdWidth, );
  273. }
  274.  
  275. StringFormat sf = new StringFormat();
  276. sf.LineAlignment = StringAlignment.Center;
  277. Font font = new Font(this.ParentGridView.Font, FontStyle.Bold);
  278. graphics.DrawString(this.Title, font, Brushes.Black,
  279. //rowBounds.Left+20,rowBounds.Top+rowBounds.Height/2,sf);
  280. rowBounds.Left - this.ParentGridView.HorizontalScrollingOffset + , rowBounds.Top + rowBounds.Height / , sf);
  281.  
  282. int symbolX = rowBounds.Left - this.ParentGridView.HorizontalScrollingOffset + ;//rowBounds.X + 5;
  283. int symbolY =rowBounds.Y+ ((rowBounds.Height - ) - ) / -;
  284. if (Application.RenderWithVisualStyles)
  285. {
  286.  
  287. VisualStyleRenderer glyphRenderer;
  288. if (this.IsExpanded)
  289. glyphRenderer = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
  290. else
  291. glyphRenderer = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Closed);
  292.  
  293. Rectangle glyphRectangle =new Rectangle(symbolX, symbolY, , );
  294.  
  295. glyphRenderer.DrawBackground(graphics, glyphRectangle);
  296.  
  297. }
  298. else
  299. {
  300. int h = ;
  301. int w = ;
  302. int x = symbolX;
  303. int y = symbolY;
  304. graphics.DrawRectangle(new Pen(SystemBrushes.ControlDark), x, y, w, h);
  305. graphics.FillRectangle(new SolidBrush(Color.White),
  306. x + , y + , w - , h - );
  307.  
  308. //画横线
  309. graphics.DrawLine(new Pen(new SolidBrush(Color.Black)),
  310. x + , y + , x + w - , y + );
  311.  
  312. //画竖线
  313. if (!this.IsExpanded)
  314. graphics.DrawLine(new Pen(new SolidBrush(Color.Black)),
  315. x + , y + , x + , y + h - );
  316. }
  317. }
  318. }

GroupGridView和GroupGridViewRow

补充部分

  上面说的那个可分组GroupGridView有一个缺憾比较致命的,就是不能排序,刚好最近工作用上了,那有工作的压力逼着就把那个排序的功能都加了上去了。

  要加这排序的,GroupGridView和GroupGridVIewRow两个类都要改,先介绍大概的思想,再分别介绍两个类。

  排序的时候不能按照往常那样比较排序,因为在GridView里面含有显示组名的,不包含数据,用它来排序会导致整个GridView会乱的。因此排序的时候需要让各个组各自排列。

  这时GroupGirdView最好就收集一下各个分组行,在排序的时候让各个分组排列

多增加两个字段

  1. protected List<GroupGridViewRow> groupCollection;
  2.  
  3. private SortOrder mySortOrder;

在绑定数据源的时候取消对各列的排列限制

  1. //foreach (DataGridViewColumn col in this.Columns)
  2. // col.SortMode = DataGridViewColumnSortMode.NotSortable;

单击单元格时也要开放一下,否则控件会忽略对行号为-1,就是列标题的处理

  1. protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
  2. {
  3. if (e.RowIndex == - || e.ColumnIndex == - || e.Button != System.Windows.Forms.MouseButtons.Left || e.Clicks != )
  4. {
  5. base.OnCellMouseDown(e);
  6. return;
  7. }
  8. //………
  9.  
  10. }

跟排序比较相关的就是重写这个方法

  1. public override void Sort(DataGridViewColumn dataGridViewColumn, System.ComponentModel.ListSortDirection direction)
  2. {
  3. //base.Sort(dataGridViewColumn, direction);
  4. foreach (GroupGridViewRow row in groupCollection)
  5. row.SortByProtery(dataGridViewColumn, mySortOrder);
  6. mySortOrder = mySortOrder == System.Windows.Forms.SortOrder.Ascending ?
  7. System.Windows.Forms.SortOrder.Descending :
  8. System.Windows.Forms.SortOrder.Ascending;
  9. }

到GroupGirdViewRow类了,这个类的改动就是按照上面的代码那样外放了一个方法,那方法就是对组内各行按升序或降序排序。

  1. public void SortByProtery(DataGridViewColumn proteryName, SortOrder order)
  2. {
  3. int colIndex = this.ParentGridView.Columns.IndexOf(proteryName);
  4. groupRows.Sort(new Comparison<DataGridViewRow>((r1, r2) =>
  5. {
  6.  
  7. object value1 = r1.Cells[proteryName.Name].Value;
  8. object value2 = r2.Cells[proteryName.Name].Value;
  9.  
  10. if (order == SortOrder.Descending) return CompareDESC(value1, value2);
  11. else return CompareASC(value1, value2);
  12.  
  13. }));
  14. int groupIndex = this.ParentGridView.Rows.IndexOf(this);
  15. foreach (DataGridViewRow row in groupRows)
  16. {
  17. this.ParentGridView.Rows.Remove(row);
  18. this.ParentGridView.Rows.Insert(groupIndex + , row);
  19. }
  20. }
  21.  
  22. private int CompareASC(object obj1, object obj2)
  23. {
  24. decimal decTmep;
  25. if (decimal.TryParse(obj1.ToString(), out decTmep) && decimal.TryParse(obj2.ToString(), out decTmep))
  26. return decimal.Compare(Convert.ToDecimal(obj1), Convert.ToDecimal(obj2));
  27. if (obj1 is DateTime && obj2 is DateTime)
  28. return DateTime.Compare(Convert.ToDateTime(obj1), Convert.ToDateTime(obj2));
  29. return string.Compare(obj1.ToString(), obj2.ToString());
  30. }
  31.  
  32. private int CompareDESC(object obj1, object obj2)
  33. {
  34. decimal decTmep;
  35. if (decimal.TryParse(obj1.ToString(), out decTmep) && decimal.TryParse(obj2.ToString(), out decTmep))
  36. return decimal.Compare(Convert.ToDecimal(obj1), Convert.ToDecimal(obj2)) * -;
  37. if (obj1 is DateTime && obj2 is DateTime)
  38. return DateTime.Compare(Convert.ToDateTime(obj1), Convert.ToDateTime(obj2)) * -;
  39. return string.Compare(obj1.ToString(), obj2.ToString()) * -;
  40. }

  其实还比较笨拙的,按自定义的排序方法对各个列在List里面拍完序之后就按照List里的新顺序重新插入到GroupGridView里面。好了,排序的功能在这里草草介绍完了,下面则是新的控件代码,除了增加了这个功能之外,还修复了一些小错误。

  1. public class GroupGridView:DataGridView
  2. {
  3. public string GroupFieldName { get; set; }
  4.  
  5. protected object objDataSource;
  6.  
  7. public GroupGridViewRow GroupRowTemplate { get;protected set; }
  8.  
  9. private bool isGroupping;
  10.  
  11. protected List<GroupGridViewRow> groupCollection;
  12.  
  13. private SortOrder mySortOrder;
  14.  
  15. public GroupGridView()
  16. {
  17. //对GridView的操作的设置
  18. this.AllowUserToAddRows = false;
  19. this.AllowUserToDeleteRows = false;
  20. this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  21.  
  22. //对GridView的样式设置
  23. this.EditMode = DataGridViewEditMode.EditProgrammatically;
  24. this.RowHeadersVisible = false;
  25. this.CellBorderStyle = DataGridViewCellBorderStyle.RaisedHorizontal;
  26.  
  27. //对实例的部分成员赋值
  28. isGroupping = false;
  29. GroupRowTemplate = new GroupGridViewRow();
  30. groupCollection = new List<GroupGridViewRow>();
  31. }
  32.  
  33. public GroupGridViewRow CreateGroupGridViewRow(string title)
  34. {
  35. GroupGridViewRow group = this.GroupRowTemplate.Clone() as GroupGridViewRow;
  36. if (group == null)
  37. throw new NullReferenceException("组模板为空或者组模板类型不是GroupGridViewRow");
  38. group.Title = title;
  39. group.ParentGridView = this;
  40. group.CreateCells(this, group.Title);
  41. this.Rows.Add(group);
  42. //do
  43. //{
  44. // group.Toggle();
  45. //} while (group.IsExpanded);
  46. group.CollapseGroup();
  47. return group;
  48. }
  49.  
  50. public new object DataSource
  51. {
  52. get
  53. {
  54. if (isGroupping) return objDataSource;
  55. return base.DataSource;
  56. }
  57. set
  58. {
  59. if (string.IsNullOrEmpty(GroupFieldName)
  60. || string.IsNullOrWhiteSpace(GroupFieldName))
  61. {
  62.  
  63. foreach (DataGridViewColumn col in this.Columns)
  64. col.SortMode = DataGridViewColumnSortMode.Automatic;
  65. base.DataSource = value;
  66. isGroupping = false;
  67. }
  68. else
  69. {
  70. //2013-10-13增加排序功能所注释
  71. //foreach (DataGridViewColumn col in this.Columns)
  72. // col.SortMode = DataGridViewColumnSortMode.NotSortable;
  73. this.Rows.Clear();
  74. this.groupCollection.Clear();
  75. if (value is IEnumerable)
  76. BindIEnumerableDataSource(value as IEnumerable);
  77. else if (value is DataTable)
  78. BindDataTableDataSouce(value as DataTable);
  79. else if (value is DataSet)
  80. BindDataSetDataSource(value as DataSet);
  81. else
  82. {
  83. throw new NotImplementedException("不支持此类型作数据源");
  84. }
  85. objDataSource = value;
  86. isGroupping = true;
  87.  
  88. }
  89. }
  90. }
  91.  
  92. protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
  93. {
  94. if (e.RowIndex == - || e.ColumnIndex == - || e.Button != System.Windows.Forms.MouseButtons.Left || e.Clicks != )
  95. {
  96. base.OnCellMouseDown(e);
  97. return;
  98. }
  99. GroupGridViewRow group = this.Rows[e.RowIndex] as GroupGridViewRow;
  100. if (group != null && group.IsIconHit(e))
  101. group.Toggle();
  102. base.OnCellMouseDown(e);
  103. }
  104.  
  105. protected override void OnCellDoubleClick(DataGridViewCellEventArgs e)
  106. {
  107. if (e.RowIndex == -||e.ColumnIndex==-)
  108. return;
  109. GroupGridViewRow group = this.Rows[e.RowIndex] as GroupGridViewRow;
  110. if (group != null )
  111. group.Toggle();
  112. base.OnCellDoubleClick(e);
  113. }
  114.  
  115. public override void Sort(DataGridViewColumn dataGridViewColumn, System.ComponentModel.ListSortDirection direction)
  116. {
  117. //base.Sort(dataGridViewColumn, direction);
  118. foreach (GroupGridViewRow row in groupCollection)
  119. row.SortByProtery(dataGridViewColumn, mySortOrder);
  120. mySortOrder = mySortOrder == System.Windows.Forms.SortOrder.Ascending ?
  121. System.Windows.Forms.SortOrder.Descending :
  122. System.Windows.Forms.SortOrder.Ascending;
  123. }
  124.  
  125. private void BindIEnumerableDataSource(IEnumerable enumerable)
  126. {
  127. IEnumerable iends = enumerable;
  128. if (iends == null) return;
  129. Type dsItemType = null;
  130. foreach (object item in iends)
  131. dsItemType = item.GetType();
  132. if (iends == null) return;
  133.  
  134. PropertyInfo proInfo = dsItemType.GetProperty(GroupFieldName);
  135.  
  136. Dictionary<string, List<object>> groupDataSource = new Dictionary<string, List<object>>();
  137. foreach (object item in iends)
  138. {
  139. string tempStr = proInfo.GetValue(item, null).ToString();
  140. if (!groupDataSource.ContainsKey(tempStr))
  141. groupDataSource[tempStr] = new List<object>();
  142. groupDataSource[tempStr].Add(item);
  143. }
  144.  
  145. List<string> colFildNames = new List<string>(this.Columns.Count);
  146. foreach (DataGridViewColumn col in this.Columns)
  147. colFildNames.Add(col.DataPropertyName);
  148. GroupGridViewRow group = null;
  149. List<object> datas = new List<object>(colFildNames.Count);
  150. foreach (KeyValuePair<string, List<object>> gi in groupDataSource)
  151. {
  152. group = CreateGroupGridViewRow(gi.Key);
  153. foreach (object celli in gi.Value)
  154. {
  155. foreach (string colName in colFildNames)
  156. {
  157. datas.Add(dsItemType.GetProperty(colName).GetValue(celli, null));
  158. }
  159. group.AddRowToGroup(datas.ToArray());
  160. datas.Clear();
  161. }
  162. //do
  163. //{
  164. // group.Toggle();
  165. //} while (group.IsExpanded);
  166. group.CollapseGroup();
  167. }
  168. }
  169.  
  170. private void BindDataTableDataSouce(DataTable table)
  171. {
  172. Dictionary<string, List<DataRow>> groupDataSource = new Dictionary<string, List<DataRow>>();
  173. foreach (DataRow row in table.Rows)
  174. {
  175. string tempStr = row[GroupFieldName].ToString();
  176. if (!groupDataSource.ContainsKey(tempStr))
  177. groupDataSource[tempStr] = new List<DataRow>();
  178. groupDataSource[tempStr].Add(row);
  179. }
  180.  
  181. List<string> colFildNames = new List<string>(this.Columns.Count);
  182. foreach (DataGridViewColumn col in this.Columns)
  183. colFildNames.Add(col.DataPropertyName);
  184. GroupGridViewRow group = null;
  185. List<object> datas = new List<object>(colFildNames.Count);
  186. foreach (KeyValuePair<string, List<DataRow>> gi in groupDataSource)
  187. {
  188. group = CreateGroupGridViewRow(gi.Key);
  189. foreach (DataRow celli in gi.Value)
  190. {
  191. foreach (string colName in colFildNames)
  192. {
  193. datas.Add(celli[colName]);
  194. }
  195. group.AddRowToGroup(datas.ToArray());
  196. datas.Clear();
  197. }
  198. //do
  199. //{
  200. // group.Toggle();
  201. //} while (group.IsExpanded);
  202. group.CollapseGroup();
  203. }
  204. }
  205.  
  206. private void BindDataSetDataSource(DataSet dataset)
  207. {
  208. if (dataset == null || dataset.Tables.Count == null) return;
  209. BindDataTableDataSouce(dataset.Tables[]);
  210. }
  211. }
  212.  
  213. public class GroupGridViewRow:DataGridViewRow
  214. {
  215. public bool IsExpanded { get;private set; }
  216.  
  217. public string Title { get; set; }
  218.  
  219. public GroupGridView ParentGridView { get; set; }
  220.  
  221. private List<DataGridViewRow> groupRows { get; set; }
  222.  
  223. public GroupGridViewRow()
  224. {
  225. IsExpanded = false;
  226. groupRows = new List<DataGridViewRow>();
  227. }
  228.  
  229. public void ExpandGroup()
  230. {
  231. IsExpanded = true;
  232. foreach (DataGridViewRow row in groupRows)
  233. row.Visible = true;
  234. }
  235.  
  236. public void CollapseGroup()
  237. {
  238. IsExpanded = false;
  239. foreach (DataGridViewRow row in groupRows)
  240. row.Visible = false;
  241. }
  242.  
  243. public void Toggle()
  244. {
  245. if (IsExpanded)
  246. CollapseGroup();
  247. else
  248. ExpandGroup();
  249. }
  250.  
  251. public bool IsIconHit(DataGridViewCellMouseEventArgs e)
  252. {
  253. Rectangle groupBound = this.ParentGridView.GetRowDisplayRectangle(e.RowIndex, false);
  254.  
  255. if (
  256. e.X > groupBound.Left + &&
  257. e.X < groupBound.Left + &&
  258. e.Y > ((groupBound.Height - ) - ) / - &&
  259. e.Y < ((groupBound.Height - ) - ) / + -
  260. )
  261. return true;
  262.  
  263. return false;
  264. }
  265.  
  266. public void SortByProtery(DataGridViewColumn proteryName, SortOrder order)
  267. {
  268. int colIndex = this.ParentGridView.Columns.IndexOf(proteryName);
  269. groupRows.Sort(new Comparison<DataGridViewRow>((r1, r2) =>
  270. {
  271.  
  272. object value1 = r1.Cells[proteryName.Name].Value;
  273. object value2 = r2.Cells[proteryName.Name].Value;
  274. //object value1 = r1.Cells[colIndex].Value;
  275. //object value2 = r2.Cells[colIndex].Value;
  276.  
  277. if (order == SortOrder.Descending) return CompareDESC(value1, value2);
  278. else return CompareASC(value1, value2);
  279.  
  280. }));
  281. int groupIndex = this.ParentGridView.Rows.IndexOf(this);
  282. foreach (DataGridViewRow row in groupRows)
  283. {
  284. this.ParentGridView.Rows.Remove(row);
  285. this.ParentGridView.Rows.Insert(groupIndex + , row);
  286. }
  287. }
  288.  
  289. private int CompareASC(object obj1, object obj2)
  290. {
  291. //if (obj1 is decimal && obj2 is decimal)//2013-10-14 正确判断数值类型
  292. decimal decTmep;
  293. if (decimal.TryParse(obj1.ToString(), out decTmep) && decimal.TryParse(obj2.ToString(), out decTmep))
  294. return decimal.Compare(Convert.ToDecimal(obj1), Convert.ToDecimal(obj2));
  295. if (obj1 is DateTime && obj2 is DateTime)
  296. return DateTime.Compare(Convert.ToDateTime(obj1), Convert.ToDateTime(obj2));
  297. return string.Compare(obj1.ToString(), obj2.ToString());
  298. }
  299.  
  300. private int CompareDESC(object obj1, object obj2)
  301. {
  302. //if (obj1 is decimal && obj2 is decimal)//2013-10-14 正确判断数值类型
  303. decimal decTmep;
  304. if (decimal.TryParse(obj1.ToString(), out decTmep) && decimal.TryParse(obj2.ToString(), out decTmep))
  305. return decimal.Compare(Convert.ToDecimal(obj1), Convert.ToDecimal(obj2)) * -;
  306. if (obj1 is DateTime && obj2 is DateTime)
  307. return DateTime.Compare(Convert.ToDateTime(obj1), Convert.ToDateTime(obj2)) * -;
  308. return string.Compare(obj1.ToString(), obj2.ToString()) * -;
  309. }
  310.  
  311. public DataGridViewRow AddRowToGroup(params object[] values)
  312. {
  313. DataGridViewRow row = this.ParentGridView.RowTemplate.Clone() as DataGridViewRow;
  314. if (row == null) throw new NullReferenceException("行模板为空或者组模板类型不是DataGridViewRow");
  315.  
  316. row.CreateCells(this.ParentGridView, values);
  317. this.ParentGridView.Rows.Add(row);
  318. this.groupRows.Add(row);
  319. return row;
  320. }
  321.  
  322. protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle rowBounds, int rowIndex, DataGridViewElementStates rowState, bool isFirstDisplayedRow, bool isLastVisibleRow)
  323. {
  324.  
  325. int holdWidth = this.ParentGridView.Columns.GetColumnsWidth(DataGridViewElementStates.Visible);
  326. Color backgroudColor;
  327. if (this.Selected)
  328. backgroudColor = this.ParentGridView.DefaultCellStyle.SelectionBackColor;
  329. else
  330. backgroudColor = this.ParentGridView.DefaultCellStyle.BackColor;
  331. using (Brush backgroudBrush = new SolidBrush(backgroudColor))
  332. {
  333. graphics.FillRectangle(backgroudBrush, rowBounds.X,rowBounds.Y,holdWidth,rowBounds.Height);
  334. }
  335. using (Brush bottomLineBrush=new SolidBrush(Color.FromKnownColor( KnownColor.GradientActiveCaption)))
  336. {
  337. graphics.FillRectangle(bottomLineBrush, rowBounds.Left, rowBounds.Top + rowBounds.Height-, holdWidth, );
  338. }
  339.  
  340. StringFormat sf = new StringFormat();
  341. sf.LineAlignment = StringAlignment.Center;
  342. Font font = new Font(this.ParentGridView.Font, FontStyle.Bold);
  343. graphics.DrawString(this.Title, font, Brushes.Black,
  344. //rowBounds.Left+20,rowBounds.Top+rowBounds.Height/2,sf);
  345. rowBounds.Left - this.ParentGridView.HorizontalScrollingOffset + , rowBounds.Top + rowBounds.Height / , sf);
  346.  
  347. int symbolX = rowBounds.Left - this.ParentGridView.HorizontalScrollingOffset + ;//rowBounds.X + 5;
  348. int symbolY =rowBounds.Y+ ((rowBounds.Height - ) - ) / -;
  349. if (Application.RenderWithVisualStyles)
  350. {
  351.  
  352. VisualStyleRenderer glyphRenderer;
  353. if (this.IsExpanded)
  354. glyphRenderer = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
  355. else
  356. glyphRenderer = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Closed);
  357.  
  358. Rectangle glyphRectangle =new Rectangle(symbolX, symbolY, , );
  359.  
  360. glyphRenderer.DrawBackground(graphics, glyphRectangle);
  361.  
  362. }
  363. else
  364. {
  365. int h = ;
  366. int w = ;
  367. int x = symbolX;
  368. int y = symbolY;
  369. graphics.DrawRectangle(new Pen(SystemBrushes.ControlDark), x, y, w, h);
  370. graphics.FillRectangle(new SolidBrush(Color.White),
  371. x + , y + , w - , h - );
  372.  
  373. //画横线
  374. graphics.DrawLine(new Pen(new SolidBrush(Color.Black)),
  375. x + , y + , x + w - , y + );
  376.  
  377. //画竖线
  378. if (!this.IsExpanded)
  379. graphics.DrawLine(new Pen(new SolidBrush(Color.Black)),
  380. x + , y + , x + , y + h - );
  381. }
  382. }
  383. }

GroupGridView和GroupGridViewRow

能分组的GridView的更多相关文章

  1. Android 使用ContentProvider扫描手机中的图片,仿微信显示本地图片效果

    版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/1873 ...

  2. Windows 8实例教程系列 - 数据绑定高级实例

    原文:Windows 8实例教程系列 - 数据绑定高级实例 上篇Windows 8实例教程系列 - 数据绑定基础实例中,介绍Windows 8应用开发数据绑定基础,其中包括一些简单的数据绑定控件的使用 ...

  3. 扩展GridView控件——为内容项添加拖放及分组功能

    引言 相信大家对GridView都不陌生,是非常有用的控件,用于平铺有序的显示多个内容项.打开任何WinRT应用或者是微软合作商的网站,都会在APP中发现GridView的使用.“Tiles”提供了一 ...

  4. Group GridView:用于.Net的分组显示的GridView

    我的项目需要一个可以分组显示的GridView,我不会写,上网找了一圈,最终在国外的网站上找到的这个,比较符合我的要求,但它的分页得重写,它写了能分页,但我发现它的分页功能事实上并没有实现,也不知道是 ...

  5. Dev GridView 获取选中分组下的所有数据行 z

    现在要在DevExpress 的GridView 中实现这样一个功能.就是判断当前的选中行是否是分组行,如果是的话就要获取该分组下的所有数据信息. 如下图(当选中红框中的分组行事.程序要获取该分组下的 ...

  6. 重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示

    原文:重新想象 Windows 8 Store Apps (12) - 控件之 GridView 特性: 拖动项, 项尺寸可变, 分组显示 [源码下载] 重新想象 Windows 8 Store Ap ...

  7. GridControl/GridView的分组操作

    今天在模块编写中碰到了对表格的分组,特意在这里把它记录下来. 一.背景:Dev14.1.3,GridControl,.NET4.0+C# 二.过程 1.GridControl设计 一共添加4列:在下面 ...

  8. Winform中GridView分组排序实现功能

    由于客户最近要扩充公司的业务,之前基于Winform+web开发混合式的系统已经不能满足他们的需求,需要从新对系统进行分区处理. 考虑到系统模块里面用到的GridView视图比较多,我就结合了DevE ...

  9. 对Dev的GridControl/GridView控件进行分组并展开操作

    今天在模块编写中碰到了对表格的分组,特意在这里把它记录下来. 一.背景:Dev14.1.3,GridControl,.NET4.0+C# 二.过程 1.GridControl设计 一共添加4列:在下面 ...

随机推荐

  1. 虚拟化平台cloudstack(7)——新版本的调试

    调试环境 ubuntu 12.04 JDK1.7 apache-maven-3.10 eclipse 4.2 Juno mysql 5 源码下载及调试 上面的几个软件在上一篇中已经介绍了. 在新的版本 ...

  2. Senparc.Weixin.MP SDK 微信公众平台开发教程(七):解决用户上下文(Session)问题

    从这篇文章中我们已经了解了微信公众平台消息传递的方式,这种方式有一个先天的缺陷:不同用户的请求都来自同一个微信服务器,这使得常规的Session无法使用(始终面对同一个请求对象,况且还有对方服务器Co ...

  3. 每天一个linux命令(56):netstat命令

    netstat命令​用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况.netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UD ...

  4. JavaScript练习之for循环语句

    for循环四要素:初始条件.循环条件.循环体.状态改变. 1.for(var a=i;i<=aa;i++) { 循环体(例sum=sum+i  sum是输出的) } 例题 1-20关没关一分 2 ...

  5. 将不确定变成确定~Uri文本文件不用浏览器自动打开,而是下载到本地

    回到目录 这个标题有点长,简单来说就是,对于一个文件下载来说,是否可以提示用户,让它去保存,而不是将它在浏览器中打开,在浏览器中打开有个致命问题,那就是,如果你的页面编码和文件的编码不一致时,打开的就 ...

  6. Lua 协程coroutine

    协程和一般多线程的区别是,一般多线程由系统决定该哪个线程执行,是抢占式的,而协程是由每个线程自己决定自己什么时候不执行,并把执行权主动交给下一个线程. 协程是用户空间线程,操作系统其存在一无所知,所以 ...

  7. [Java面试十二]数据库概念相关

    1. 什么是存储过程?它有什么优点? 答:存储过程是一组予编译的SQL语句,它的优点有:     允许模块化程序设计,就是说只需要创建一次过程,以后在程序中就可以调用该过程任意次.     允许更快执 ...

  8. [CSS]复选框单选框与文字对齐问题的研究与解决.

    前言:今天碰到的这个问题, 恰好找到一个很好的博文, 在这里转载过来 学习下. 原文地址:复选框单选框与文字对齐问题的研究与解决. 目前中文网站上面的文字,就我的个人感觉而言,绝大多数网站的主流文字大 ...

  9. Android笔记——探究活动

    1.活动是什么       活动(Activity)是最容易吸引到用户的地方了,它是一种可以包含用户界面的组件,主要用于和用户进行交互.一个应用程序中可以包含零个或多个活动,但不包含任何活动的应用程序 ...

  10. C# string.format、string.connect和+=运算 效率计算

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Stri ...