Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 根据单元格值更改对Datagridview重新排序_Vb.net - Fatal编程技术网

Vb.net 根据单元格值更改对Datagridview重新排序

Vb.net 根据单元格值更改对Datagridview重新排序,vb.net,Vb.net,我当前在单元格值更改后尝试重新排序datagridview时遇到问题。在特定列中的值发生更改后,我希望基于此新值使用列。然而,它的排序并不完全正确 例如,假设我从下图中的DataGridVew开始。 然后对第一行的Line列进行更改,并将其重新编号为8。此时,我想将第1行移动到当前第8行的位置,然后每隔一行重新编号 我已经实现了以下代码来实现这一点 Dim OuterSourceRowIndex As Integer Dim InnerSourceRowIndex As Int

我当前在单元格值更改后尝试重新排序datagridview时遇到问题。在特定列中的值发生更改后,我希望基于此新值使用列。然而,它的排序并不完全正确

例如,假设我从下图中的DataGridVew开始。

然后对第一行的Line列进行更改,并将其重新编号为8。此时,我想将第1行移动到当前第8行的位置,然后每隔一行重新编号

我已经实现了以下代码来实现这一点

    Dim OuterSourceRowIndex As Integer
    Dim InnerSourceRowIndex As Integer

    If e.ColumnIndex = 0 Then
        For Each rowOuter As DataGridViewRow In dgvReceive.Rows
            For Each rowInner As DataGridViewRow In dgvReceive.Rows
                OuterSourceRowIndex = rowOuter.Index
                InnerSourceRowIndex = rowInner.Index
                If rowOuter.Cells(0).Value >= rowInner.Cells(0).Value Then
                    dgvReceive.Rows.RemoveAt(OuterSourceRowIndex)
                    dgvReceive.Rows.Insert(InnerSourceRowIndex, rowOuter)
                End If
            Next
        Next

        For Each row As DataGridViewRow In dgvReceive.Rows
            row.Cells(0).Value = row.Index + 1
        Next
    End If
当我尝试实际进行更改时,尽管我最终得到了以下结果,其中第1行实际移动到了位置7。所有这些都发生在CellValueChanged事件上


如果以前有人回答过,我很抱歉。我找不到任何像这样的东西。感谢您提供的帮助。

您可能已经使这些双循环更加复杂。此外,您应该避免为每个循环创建一个For-Each循环,因为您在循环集合时正在修改它

关于错误检查的内容不多,但这是一个简化版本。它删除您更改的行,将其插入正确的位置,然后对所有内容重新编号

If e.ColumnIndex = 0 Then
  RemoveHandler dgvReceive.CellValueChanged, AddressOf dgvReceive_CellValueChanged
  Dim thisRow As DataGridViewRow = dgvReceive.Rows(e.RowIndex)
  dgvReceive.Rows.Remove(thisRow)
  dgvReceive.Rows.Insert(CInt(thisRow.Cells(0).Value) - 1, thisRow)
  For Each row As DataGridViewRow In dgvReceive.Rows
    row.Cells(0).Value = row.Index + 1
  Next
  AddHandler dgvReceive.CellValueChanged, AddressOf dgvReceive_CellValueChanged
End If

我添加了RemoveHandler和AddHandler行,以防止在对行项目重新编号时更改所有单元格值时触发过多的焰火。

DGV的数据源是什么?如果它是DataTable,则可以使用
DataTable.DefaultView.Sort
属性(例如
…=“Line ASC”
)。DataGridView控件还有一个
Sort()
方法(例如
dataGridView1.Sort(Column1,ListSortDirection.Ascending)
)。请注意,列的数据类型决定排序顺序。@Jimi它是一个未绑定的datagridview。我将研究sort方法,但是从我尝试的非常快速的实现来看,我无法以我想要的方式实现列重新编号。我可能会草率地阅读它。您对单元格重新编号有问题。如果修改范围
((a-1):(B-1))
,则距离为
D=(B-a)
,方向为
a=(V(B)>V(a)?1:-1)
。因此,如果8变为2,则为
((8-1)-(2-1))行重新编号
(2-1)
。由于
V(b)>V(a)
8
2
),您可以向范围中的值添加+1,然后将新值设置为已更改的行并排序()。这非常有效。你是对的,因为每个都是一个坏主意,所以我试图单步执行这部分代码。当我知道意外事件会运行时,我从未想过删除和添加处理程序。有趣。谢谢。@Mary最好将此代码放在Try-Catch-Finally块中,并将AddHandler放在Finally块中,以确保事件订阅不会丢失。明白了。谢谢