1. /// <summary>
    2. /// 上移
    3. /// </summary>
    4. /// <param name="dataGridView"></param>
    5. public static void UpDataGridView(DataGridView dataGridView)
    6. {
    7. try
    8. {
    9. int count = dataGridView.SelectedRows.Count;
    10. if (count==0)
    11. {
    12. return;
    13. }
    14. int index = dataGridView.SelectedRows[count - 1].Index;//DataGridView中,选中的第一条
    15. if (index <= 0)//选中开头
    16. {
    17. return;
    18. }
    19. List<DataGridViewRow> rows = new List<DataGridViewRow>();
    20. foreach (DataGridViewRow row in dataGridView.SelectedRows)
    21. {
    22. rows.Add(row);
    23. }
    24. //说明:DataGridView.SelectedRows倒序排列DataGridView选中的行
    25. rows.Reverse();//反转顺序,改为正序
    26. List<int> rowIndex = new List<int>();//保存已经选中位移后的下标
    27. for (int i = 0;i< rows.Count;i++)
    28. {
    29. int saveIndex = rows[i].Index;
    30. rowIndex.Add(saveIndex-1);
    31. dataGridView.Rows.RemoveAt(saveIndex);//删除
    32. dataGridView.Rows.Insert(saveIndex - 1, rows[i]);//添加到指定位置
    33. }
    34. dataGridView.ClearSelection();//清理其他选中
    35. foreach (var item in rowIndex)//重新设置选中
    36. {
    37. dataGridView.Rows[item].Selected = true;
    38. }
    39. }
    40. catch (Exception ex)
    41. {
    42. MessageBox.Show(ex.ToString());
    43. }
    44. }
    45. /// <summary>
    46. /// 下移
    47. /// </summary>
    48. /// <param name="dataGridView"></param>
    49. public static void DownDataGridView(DataGridView dataGridView)
    50. {
    51. try
    52. {
    53. int count = dataGridView.SelectedRows.Count;
    54. if (count == 0)
    55. {
    56. return;
    57. }
    58. int index = dataGridView.SelectedRows[0].Index;//DataGridView中,选中的最后一条
    59. if (index >= dataGridView.Rows.Count-1)//选中结尾
    60. {
    61. return;
    62. }
    63. List<DataGridViewRow> rows = new List<DataGridViewRow>();
    64. foreach (DataGridViewRow row in dataGridView.SelectedRows)
    65. {
    66. rows.Add(row);
    67. }
    68. List<int> rowIndex = new List<int>();//保存已经选中位移后的下标
    69. for (int i = 0; i < rows.Count; i++)
    70. {
    71. int saveIndex = rows[i].Index;
    72. rowIndex.Add(saveIndex + 1);
    73. dataGridView.Rows.RemoveAt(saveIndex);//删除
    74. dataGridView.Rows.Insert(saveIndex + 1, rows[i]);//添加到指定位置
    75. }
    76. dataGridView.ClearSelection();//清理其他选中
    77. rowIndex.Reverse();//反转,为了选中顺序和默认选中顺序保存一致
    78. foreach (var item in rowIndex)//重新设置选中
    79. {
    80. dataGridView.Rows[item].Selected = true;
    81. }
    82. }
    83. catch (Exception ex)
    84. {
    85. MessageBox.Show(ex.ToString());
    86. }
    87. }
    88. /// <summary>
    89. /// 删除
    90. /// </summary>
    91. /// <param name="dataGridView"></param>
    92. public static void DeleteDataGridView(DataGridView dataGridView)
    93. {
    94. try
    95. {
    96. int count = dataGridView.SelectedRows.Count;
    97. if (count == 0)
    98. {
    99. return;
    100. }
    101. int index = dataGridView.SelectedRows[count - 1].Index;//DataGridView中,选中的第一条
    102. if (index < 0)
    103. {
    104. return;
    105. }
    106. DialogResult dr = MessageBox.Show("确定删除当前选中行?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
    107. if (dr == DialogResult.OK)
    108. {
    109. List<int> rowIndex = new List<int>();//保存的下标
    110. foreach (DataGridViewRow row in dataGridView.SelectedRows)
    111. {
    112. rowIndex.Add(row.Index);
    113. }
    114. foreach (var item in rowIndex)
    115. {
    116. dataGridView.Rows.RemoveAt(item);
    117. }
    118. }
    119. }
    120. catch (Exception ex)
    121. {
    122. MessageBox.Show(ex.ToString());
    123. }
    124. }