Posted By

sekihin on 09/01/11


Tagged

Winform DataGridView c#


Versions (?)

Maintaining Scroll Position after sort


 / Published in: C#
 

  1. private int HScrollBarPos = 0;
  2. private bool HPosFLG = false;
  3. private void GetSBarPos(Emico.Common.UI.EmiDataGridView dg)
  4. {
  5. HPosFLG = false;
  6.  
  7. foreach (Control c in dg.Controls)
  8. {
  9. if (c.GetType().Name == "HScrollBar")
  10. {
  11. ScrollBar sBar = (ScrollBar)c;
  12. HScrollBarPos = sBar.Value;
  13. HPosFLG = true;
  14. }
  15. }
  16. }
  17.  
  18. private void SetSBarPos(Emico.Common.UI.EmiDataGridView dg)
  19. {
  20. foreach (Control c in dg.Controls)
  21. {
  22. if (c.GetType().Name == "HScrollBar")
  23. {
  24. if (HPosFLG)
  25. {
  26. dg.HorizontalScrollingOffset = HScrollBarPos;
  27. }
  28. }
  29. }
  30. }
  31.  
  32. private void emiDataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  33. {
  34.  
  35. int aCol = emiDataGridView1.ActiveColumnIndex;
  36. int aRow = emiDataGridView1.ActiveRowIndex;
  37. emiDataGridView1.SuspendLayout();
  38.  
  39.  
  40. GetSBarPos(emiDataGridView1);
  41.  
  42. // 行を移動させて、編集を確定させます。(EndEdit ではうまくいかない・・・)
  43. if (emiDataGridView1.CurrentCell != null && emiDataGridView1.CurrentCell.IsInEditMode == true)
  44. {
  45. if (emiDataGridView1.ActiveRowIndex == 0)
  46. {
  47. if (emiDataGridView1.RowCount > 1)
  48. {
  49. if (emiDataGridView1.ActiveRowIndex != 0)
  50. {
  51. emiDataGridView1.ActiveRowIndex++;
  52. }
  53. }
  54. else
  55. {
  56. if (emiDataGridView1.ActiveColumnIndex == 0)
  57. {
  58. emiDataGridView1.ActiveColumnIndex = 1;
  59. }
  60. else
  61. {
  62. emiDataGridView1.ActiveColumnIndex = 0;
  63. }
  64. }
  65. }
  66. //emiDataGridView1.CurrentCell = emiDataGridView1[0, 0];
  67. }
  68.  
  69. string aColName = emiDataGridView1.Columns[e.ColumnIndex].Name;
  70.  
  71. // 現在のソート順から新しいソート順を判定。
  72. SortOrder nowOrder = emiDataGridView1.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection;
  73. SortOrder newOrder;
  74. string strOrder;
  75. if (nowOrder == SortOrder.Ascending)
  76. {
  77. newOrder = SortOrder.Descending;
  78. strOrder = " DESC";
  79. }
  80. else
  81. {
  82. newOrder = SortOrder.Ascending;
  83. strOrder = " ASC";
  84. }
  85.  
  86. // 列ヘッダーのグリフ(矢印)を、一旦クリア。
  87. emiDataGridView1.Columns["ColA"].HeaderCell.SortGlyphDirection = SortOrder.None;
  88. emiDataGridView1.Columns["ColB"].HeaderCell.SortGlyphDirection = SortOrder.None;
  89. emiDataGridView1.Columns["ColC"].HeaderCell.SortGlyphDirection = SortOrder.None;
  90.  
  91. // ソート文字列を生成。
  92. StringBuilder sbSort = new StringBuilder();
  93. if (aColName.Length > 0)
  94. {
  95. sbSort.Append(aColName);
  96. sbSort.Append(strOrder);
  97. }
  98.  
  99. // ソート。
  100. _tblTestclass.DefaultView.Sort = sbSort.ToString();
  101.  
  102. // グリフ(矢印)を設定。
  103. if (aColName.Length > 0)
  104. {
  105. emiDataGridView1.Columns[aColName].HeaderCell.SortGlyphDirection = newOrder;
  106. }
  107.  
  108. if (aCol >= 0 && aRow >= 0)
  109. {
  110. emiDataGridView1.CurrentCell = emiDataGridView1[e.ColumnIndex, 0];
  111. }
  112.  
  113. SetSBarPos(emiDataGridView1);
  114. emiDataGridView1.Refresh();
  115. emiDataGridView1.ResumeLayout();
  116. }

Report this snippet  

You need to login to post a comment.