Grouping data rows in asp.net 2.0 VB

As the name implies, I'm trying to group strings into a datatable. For a more detailed study, this table has the same rows, with the exception of one field (column). Basically what I'm trying to do is put all the different fields of the same lines and put them in one field, deleting the other lines.

Here is the syntax that I am currently using

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 deleting rows whose index is changing, and basically the field gets another row that has nothing to do with it.

+3
source share
5 answers

Well, I can help with the loop structure. This does not match what you are doing exactly (it leaves the table intact and just creates a large row, and also assumes the table is sorted in a certain way), but it will demonstrate the classic interrupt handling using your actual data. For this to work, the table must be sorted by the user, and then the 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 

It has 3 nested loops, not just your two. However, it still gets linear performance: it will only process each record once. It works because all loops have the same counter, which only increases in the inner loop, and they all have the same basic end condition.

+1
source

I needed to do something similar recently with a control style report.

I ended up attaching data to the relay controller, where the element template was just literal. Then I handled the OnItemDataBound event and had code that vaguely resembled yours, to check that until the control column (in the sense of control-blur, and not in the sense of web control) matches, just add the value of the remaining column into a class variable level and set e.Item.Visible to false. When they no longer match, I leave e.Item.Visible as true (the default) and set the Text property of the literal element to the html needed for the line I was working on.

I'm sure there is a better way to do this, but it’s just not easy to find information about control abort reports using asp.net: the word control has a double meaning when searching in this domain, which is difficult to overcome.

0
source

It is rather difficult to read, which usually means that he is ready for restructuring.

Can you still outline what you want to achieve? What do you want to leave in your DataTable?

Is the data collected sorted or can you do it in db? If it is sorted, you only need to go through one cycle.

I would do the following:

  • Sort datatable
  • Create a new data type for your results.
  • step by step, saving the last username and current role list variable. If the username does not match the last, add a new row to your datatable with the last username and the current list of roles. If the username matches, add it to the list of roles. Remember to add the last line when you get to the end.

This fixes the problem of deleting rows from the collection you are going through.

0
source

Well, apparently my code is working. Line i - = 1 was optional. Sorry for the inconvenience and thanks for the help.

0
source

The cyclic part looks as if it is closed.

But deleting elements is selective (or all), while a cycle can become unpleasant if you do not understand that the trick should work only from the bottom up (or from the last element to the first, or you want to track it down).

That the whole answer is for a deletion of the question.

When you work from the last to the first, deleting an item in a numbered list only affects the indices of those items that you have already processed - and who cares then, right?

-T.

0
source

All Articles