VB.NET - 在特定索引处将行插入DataView或DataGrid

时间:2016-06-03 14:47:52

标签: vb.net sorting datagrid dataview

我有一行 DataView 。该数据在被用作打印到网页的 DataGrid 的来源之前正在进行排序和过滤。有时,即使它们不是完美的排序顺序,也应该将成对的行组合在一起。我的问题是我不知道如何从默认排序中的任何位置删除该行并将其重新插入到不同的索引中。

例如,假设我有这些数据:

App# | Name | State | Status | Creator | LinkID
______________________________________________
1001 | Joe  |  LA   |  New   |  User1  | 1234
0123 | Jan  |  NE   | Closed |  User2  | 
0455 | Sue  |  NY   |  New   |  User3  | 1234
0080 | Bob  |  CA   | Closed |  User4  | 
0001 | Ron  |  ND   |  New   |  User5  | 

我需要始终将LinkID 1234组合在一起的两行。因此,如果用户按名称排序,例如,这就是我们得到的结果:

App# | Name | State | Status | Creator | LinkID
______________________________________________
0080 | Bob  |  CA   | Closed |  User4  | 
0123 | Jan  |  NE   | Closed |  User2  | 
1001 | Joe  |  LA   |  New   |  User1  | 1234
0455 | Sue  |  NY   |  New   |  User3  | 1234
0001 | Ron  |  ND   |  New   |  User5  | 

注意Sue是如何失序的。为了实现这一点,我需要在数据排序后循环回来,并将任何链接的行移动到匹配的位置。

我这样做的想法看起来像这样:

Dim dvSource As DataView = DS.Tables("Request").DefaultView
dvSource.Sort = sortString
dvSource.RowFilter = filterString

dgGrid.DataSource = dvSource
dgGrid.DataBind()

'Loop through datagrid and move linked rows together
For i = 0 To dgGrid.Items.Count - 1
    If dgGrid.Items(i).Cells(5).Text <> "&nbsp;" Then
        For j As Integer = i + 1 To dgGrid.Items.Count - 1
            If dgGrid.Items(i).Cells(5).Text = dgGrid.Items(j).Cells(5).Text Then
                Dim row As DataGridItem = dgGrid.Items(j)
                'Remove row from dgGrid
                'Insert row into dgGrid at new index
            End If
        Next
    End If
Next

如何从dgGrid中删除行并插入新索引?

1 个答案:

答案 0 :(得分:0)

要删除和插入行,您可以使用以下代码:

Dim Row as DataRow
Row = dgGrid.Rows(x) 'Where x is the index of the row you want to remove
dgGrid.Rows.Remove(Row)
dgGrid.Rows.InsertAt(row, y) 'Where y is the index where you want the row to be inserted