【发布时间】:2015-06-14 18:20:06
【问题描述】:
我有一个这样的 DataGridView:
现在,在 C# 或 VB.Net 中,我想用一个按钮将所选行向上或向下移动一个位置,我该怎么做?.多选要求使我复杂化了。
DataGridView中出现的文件会被合并成一个可执行文件,然后文件会按照“Order”列的顺序执行,所以“Order " 值在向上或向下移动行时应该是不可变的。
我没有使用任何数据源。
我试图从 MSDN 分析这个示例,它包含一些扩展方法,但它需要一个数据表和数据源,我对使用数据表和数据源没有任何不满,但是只是我不知道如何为我的 DataGridView 调整代码示例。 无论如何,示例不支持多选:
Move rows up/down and remember order for DataGridView and ListBoxs data bound
我还在 StackOverflow 上看到了一些关于此的 C# 问题,但他们要求选择单行:
How to move gridview selected row up/down on KeyUp or Keydown press
DataGridView Selected Row Move UP and DOWN
然后,我没有起点,只有这些方法可以移动一个也不保留 Order 值的单行,如果有人可以指导我根据我的需要扩展功能: Private Sub Button_MoveUp_Click(sender As Object, e As EventArgs) _ 处理 Button_MoveUp.Click
Me.MoveUpSelectedRows(Me.DataGridView_Files)
End Sub
Private Sub Button_MoveDown_Click(sender As Object, e As EventArgs) _
Handles Button_MoveDown.Click
Me.MoveDownSelectedRows(Me.DataGridView_Files)
End Sub
Private Sub MoveUpSelectedRows(ByVal dgv As DataGridView)
Dim curRowIndex As Integer = dgv.CurrentCell.RowIndex
Dim newRowIndex As Integer = curRowIndex - 1
Dim curColIndex As Integer = dgv.CurrentCell.ColumnIndex
Dim curRow As DataGridViewRow = dgv.CurrentRow
If (dgv.SelectedCells.Count > 0) AndAlso (newRowIndex >= 0) Then
With dgv
.Rows.Remove(curRow)
.Rows.Insert(newRowIndex, curRow)
.CurrentCell = dgv(curColIndex, newRowIndex)
End With
End If
End Sub
Private Sub MoveDownSelectedRows(ByVal dgv As DataGridView)
Dim curRowIndex As Integer = dgv.CurrentCell.RowIndex
Dim newRowIndex As Integer = curRowIndex + 1
Dim curColIndex As Integer = dgv.CurrentCell.ColumnIndex
Dim curRow As DataGridViewRow = dgv.CurrentRow
If (dgv.SelectedCells.Count > 0) AndAlso (dgv.Rows.Count > newRowIndex) Then
With dgv
.Rows.Remove(curRow)
.Rows.Insert(newRowIndex, curRow)
.CurrentCell = dgv(curColIndex, newRowIndex)
End With
End If
End Sub
更新
我正在尝试@Plutonix 方法(稍作修改),唯一的问题是它不能正确地将所选行向上移动。
重现问题的步骤:
选择两个在一起的行(例如,行索引 2 和行索引 3,NOT 行索引 2 和行索引 4)
尝试将行向上移动。
我该如何解决?
Public Enum MoveDirection As Integer
Up = -1
Down = 1
End Enum
Private Sub MoveRows(ByVal dgv As DataGridView, ByVal moveDirection As MoveDirection)
Dim rows As DataGridViewRowCollection = dgv.Rows
' row index
Dim thisRow As DataGridViewRow
' put selection back
Dim selectedRows As New List(Of Integer)
' max rows
Dim lastRowIndex As Integer =
If(dgv.AllowUserToAddRows,
rows.Count - 2,
rows.Count - 1)
For n As Integer = lastRowIndex To 0 Step -1
If Not rows(n).IsNewRow Then
If rows(n).Selected Then
selectedRows.Add(n)
MsgBox(n)
Select Case moveDirection
Case Main.MoveDirection.Down
If ((n + moveDirection) <= lastRowIndex) AndAlso (n + moveDirection >= 0) AndAlso rows(n + moveDirection).Selected = False Then
selectedRows(selectedRows.Count - 1) = (n + moveDirection)
thisRow = rows(n)
rows.Remove(thisRow)
rows.Insert(n + moveDirection, thisRow)
End If
Case Main.MoveDirection.Up
If ((n + moveDirection) <= lastRowIndex) Then
MsgBox(selectedRows(selectedRows.Count - 1))
selectedRows(selectedRows.Count - 1) = (n + moveDirection)
thisRow = rows(n)
rows.Remove(thisRow)
rows.Insert(n + moveDirection, thisRow)
End If
End Select
End If
End If
Next n
' reselect the original selected rows
For n As Integer = 0 To lastRowIndex
dgv.Rows(n).Selected = selectedRows.Contains(n)
' renumber the order (optional & unknown, but trivial)
dgv.Rows(n).Cells(0).Value = (n + 1)
Next n
End Sub
【问题讨论】:
-
“订单”值应该是不可变的是什么意思?当这 2 行向下移动 1 行时,它们是 (2, 4) 还是 (3, 5)?不可变意味着它们不会改变,这意味着可见订单与订单显示的数字不匹配。这似乎令人困惑。
-
是的,可见索引不应该改变,你完全理解我的意思,别担心:),谢谢。
标签: c# .net vb.net winforms datagridview