分组行的数据表在VB asp.net 2.0 [英] Grouping rows of a datatable in VB asp.net 2.0

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

问题描述

,顾名思义我想组行的数据表。为了进一步详述此表有只有一个行业例外(列)相同的行。基本上,我想要做的就是把同一行的所有不同的领域,并把它们放在单一的领域,而删除其他行。

As the name suggests I am trying to group rows in a datatable. To go into further detail this table has identical rows except for one field(column). Basically what I am trying to do is put all the different fields of the identical rows and put them in single field whilst deleting the other rows.

下面是我目前使用语法

   Dim i As Integer
    Dim j As Integer
    For i = 0 To (ds.Tables(0).Rows.Count() - 1) Step 1
        If (i < ds.Tables(0).Rows.Count()) Then
            roleHtml = "<table><tr><td>" + ds.Tables(0).Rows(i).Item("roleName") + "</td></tr>"
            For j = (ds.Tables(0).Rows.Count() - 1) To 0 Step -1
                If (ds.Tables(0).Rows(i).Item("UserName") = ds.Tables(0).Rows(j).Item("UserName")) And (ds.Tables(0).Rows(i).Item("roleName") IsNot ds.Tables(0).Rows(j).Item("roleName")) Then
                    roleHtml += "<tr><td>" + ds.Tables(0).Rows(j).Item("roleName") + "</td></tr>"
                    ds.Tables(0).Rows.Remove(ds.Tables(0).Rows(j))
                    i -= 1
                End If
            Next j
            roleHtml += "</table>"
            ds.Tables(0).Rows(i).Item("roleName") = roleHtml
        End If
    Next i

现在的问题是删除行,当他们的指数变化,基本上是场被扔在没有任何与它的另一行。

The problem is when deleting the rows their index changes and basically the field gets thrown in another row that has nothing to do with it.

推荐答案

嗯,我可以帮助解决循环结构。这不符合你正在做的究竟是什么(它使表完好,只需建立一个大的字符串,并且还采用表进行排序以特定的方式),但它会表现出经典的控制断处理用实际数据。对于这个工作表需由用户,然后作用。

Well, I can help with the looping structure. This doesn't match what you're doing exactly (it leaves the table intact and just builds a big string, and also assumes the table is sorted a particular way), but it will demonstrate classic control-break processing using your actual data. For this to work the table needs to be sorted by user and then role.

Dim i As Integer = 0
Dim CurUser As String = ""
Dim CurRole As String = ""
Dim result As new StringBuilder()
Dim r as DataRowCollection = ds.Tables(0).Rows

While i < r.Count
    'Next User:'
    CurUser = r(i)("UserName")
    result.AppendFormat("<h2>{0}</h2>", CurUser).AppendLine()
    result.AppendLine("<table>")

    While i < r.Count AndAlso CurUser = r(i)("UserName")
        'Next Role:'
        CurRole = r(i)("roleName")
        result.AppendFormat("<tr><td>{0}</td></tr>", CurRole).AppendLine()

        While i < r.Count AndAlso CurUser = r(i)("UserName") AndAlso CurRole = r(i)("roleName")
            i += 1 'Next Record: same user, role '
        End While
        'Finished this role'
    End While
    'Finished this user:'
     result.AppendLine("</table>").AppendLine()
End While

这具有的 3 嵌套循环,而不只是你的两个。但是,它仍然得到的线性的性能:它只会每条记录遍历一次。它的工作原理是因为所有的循环共享相同的计数器,这是只递增在内部循环,它们都具有相同的基本结束条件

This has 3 nested loops, rather than just your two. However, it still gets linear performance: it will only iterate over each record once. It works because all the loops share the same counter, which is only incremented in the inner loop, and they all have the same base end condition.

这篇关于分组行的数据表在VB asp.net 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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