【问题标题】:VB.NET - Insert row into DataView or DataGrid at specific indexVB.NET - 在特定索引处将行插入 DataView 或 DataGrid
【发布时间】:2016-06-03 14:47:52
【问题描述】:

我有一个充满行的 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 中删除行并在新索引处插入?

【问题讨论】:

    标签: vb.net sorting datagrid dataview


    【解决方案1】:

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

    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 
    

    【讨论】:

    • Rows 不是DataGrid 的成员。
    • 尝试在项目顶部添加Imports System.Data
    • 它已经在那里了。 Visual Studio 也恰好告诉我 Imports 语句是不必要的......
    • MSDN 也没有显示对 Rows 属性的任何引用:msdn.microsoft.com/en-us/library/…
    • OP 的网格正在使用 Items 集合。它是与 DataGridView 不同的网格。
    猜你喜欢
    • 2019-01-01
    • 2020-05-28
    • 1970-01-01
    • 2011-05-17
    • 2023-04-08
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多