VB.NET在SORTED数据表中上下移动行 [英] VB.NET Shifting row up/down in SORTED datatable

查看:137
本文介绍了VB.NET在SORTED数据表中上下移动行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建在datagridview中向上或向下移动的函数,它是从datatable dtProc提供的。在unbound datagridview上很容易,但是我试图用datasource = dtProc来实现它。问题是,我排序对于RowNo(正确的顺序)的dtProc。功能波纹管工作,但只有一次。第二次移动原始位置的行,而不是希望的行,向上移动1行。索引是正确的,但未排序的dtRows(执行操作)具有不同的顺序,然后显示,排序为dtRows。

  Private Sub ShiftUp()
如果Me.dgwNew.SelectedRows.Count> 0然后'如果选择存在
Dim selIdx As Int16 = Me.dgwNew.SelectedRows(0).Index'selected index
Dim secIdx As Int16 = Me.dgwNew.SelectedRows(0).Index - 1'第二个索引
MsgBox(selIdx =& selIdx&secIdx =& secIdx)'测试反馈
dtProc.DefaultView.Sort =(RowNo ASC)'试图保持排序顺序
dtProc.Rows(selIdx)(RowNo)= secIdx + 1'与第二个
dtProc.Rows(secIdx)(RowNo)= selIdx + 1'交换第二个索引与第二个
调用RefreshDgw()
Me.dgwNew.ClearSelection()'清除datagridview选择
Me.dgwNew.CurrentCell = Me.dgwNew.Rows(secIdx).Cells(0)
Me.dgwNew.Rows(secIdx).Selected = True'在二级行上设置选择
如果
End Sub

Private Sub RefreshDgw()
dtProc.DefaultView .Sort = (RowNo ASC)
Me.dgwNew.DataSource = dtProc.DefaultView.ToTable
End Sub

有没有办法让它工作?



编辑 - 最终解决方案:



我无法使用DataView,问题仍然存在。所以我不得不添加一个额外的函数,它根据DataView中的行索引在DataTable中获取行索引。

 私有函数GetDataTableRowID (rowIdx)
Dim RetVal As Int16
对于ir = 0到dtProc.Rows.Count - 1
如果rowIdx + 1 = dtProc.Rows(ir)(RowNo)然后
RetVal = ir
退出
结束如果
下一个
返回RetVal
结束函数

然后我修改了两行,其中我设置索引如下:

  Dim selIdx As Int16 = Me.dgwNew.SelectedRows(0).Index'selected DataView索引
Dim secIdx As Int16 = Me.dgwNew.SelectedRows(0).Index - 1'第二个DataView索引

Dim selIdx2 As Int16 = GetDataTableRowID(selIdx)'selected DataTable index
Dim secIdx2 As Int16 = GetDataTableRowID(secIdx)'second DataTable index

dtProc.Rows(selIdx2)(RowNo )= secIdx + 1'与第二个
交换选择的索引dtProc.Rows(secIdx2)(RowNo)= selIdx + 1'选择的
交换第二个索引
pre>

解决方案


  1. 在您的DataTable中添加一个整数列称为序列

  2. 将您的表DefaultView.Sort设置为序列
  3. 创建一个 DataView 最初在RowNo上排序数据

  4. 迭代视图中的行将Sequence分配给行索引

  5. 将您的表绑定到您的DataGridView

  6. 更改表行中的序列值以上下移动行


I am trying to create functions to shift up or down row in datagridview, which is fed from datatable dtProc. It would be easy on an unbound datagridview, but I am trying to achieve it with datasource = dtProc. The problem is, that I sort the dtProc accordint to RowNo (a correct order). The function bellow works, but only ONCE. Second time it shifts rows in the original position, not the desired rows, which moved up by 1 row. The indexes are correct, but the unsorted dtRows (on which the operation is performed) has different order then displayed, sorted dtRows.

Private Sub ShiftUp()
    If Me.dgwNew.SelectedRows.Count > 0 Then            ' if selection exists
        Dim selIdx As Int16 = Me.dgwNew.SelectedRows(0).Index      ' selected index
        Dim secIdx As Int16 = Me.dgwNew.SelectedRows(0).Index - 1  ' second index
        MsgBox("selIdx=" & selIdx & "     secIdx=" & secIdx)       ' test feedback
        dtProc.DefaultView.Sort = ("RowNo ASC")         ' trying to keep sorted order
        dtProc.Rows(selIdx)("RowNo") = secIdx + 1       ' swap selected index with second
        dtProc.Rows(secIdx)("RowNo") = selIdx + 1       ' swap second index with selected
        Call RefreshDgw()
        Me.dgwNew.ClearSelection()                      ' clear datagridview selection
        Me.dgwNew.CurrentCell = Me.dgwNew.Rows(secIdx).Cells(0)
        Me.dgwNew.Rows(secIdx).Selected = True          ' set selection on secondary row
    End If
End Sub

Private Sub RefreshDgw()
    dtProc.DefaultView.Sort = ("RowNo ASC")
    Me.dgwNew.DataSource = dtProc.DefaultView.ToTable
End Sub

Is there a trick to make it work?

EDIT - FINAL SOLUTION:

I was unable to work with DataView either, the problem persisted. So I had to add an extra function, which gets row index in DataTable according to a row index in DataView.

Private Function GetDataTableRowID(rowIdx)
    Dim RetVal As Int16
    For ir = 0 To dtProc.Rows.Count - 1 
        If rowIdx + 1 = dtProc.Rows(ir)("RowNo") Then
            RetVal = ir
            Exit For
        End If
    Next
    Return RetVal
End Function

Then I modified the two lines, where I set indexes as follows:

        Dim selIdx As Int16 = Me.dgwNew.SelectedRows(0).Index      ' selected DataView index
        Dim secIdx As Int16 = Me.dgwNew.SelectedRows(0).Index - 1  ' second DataView index

        Dim selIdx2 As Int16 = GetDataTableRowID(selIdx)      ' selected DataTable index
        Dim secIdx2 As Int16 = GetDataTableRowID(secIdx)      ' second DataTable index

        dtProc.Rows(selIdx2)("RowNo") = secIdx + 1       ' swap selected index with second
        dtProc.Rows(secIdx2)("RowNo") = selIdx + 1       ' swap second index with selected

解决方案

  1. Add an Integer column to your DataTable called Sequence
  2. Set your table DefaultView.Sort to Sequence
  3. Create a DataView that initially sorts your data on RowNo
  4. Iterate the rows in the view assigning Sequence to row index
  5. Bind your table to your DataGridView
  6. Change the Sequence value in the table rows to move rows up/down

这篇关于VB.NET在SORTED数据表中上下移动行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆