-
Aug 22nd, 2015, 06:28 PM
#1
Thread Starter
Member
-
Aug 22nd, 2015, 07:27 PM
#2
Re: Partition and Rank datatable by a certain column
So you want to sort by Type alphabetically ascending, then you want to rank each entry by LoanAmt. In your attached image example, would that be 1 to 10 ranking or 1 to 5 (for A Types) and 1 to 5 (for B Types)?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 23rd, 2015, 01:18 AM
#3
Thread Starter
Member
Re: Partition and Rank datatable by a certain column
Hi Paul
Its a 1 to 5 ranking for type A and similar for Type B and the Ranks will be in the Rnk column.
How is it done?
A function like this one in the link below but for different Types
http://code-programmming.blogspot.co...c-example.html
Last edited by gbhs; Aug 23rd, 2015 at 03:17 AM.
-
Aug 23rd, 2015, 02:59 AM
#4
Re: Partition and Rank datatable by a certain column
Hi,
From what I understand of your question, I would use a combination of LINQ and traditional For Loops here to make things easier to read and understand.
In your case you need to Group your DataRows by the Type column, Order the LoanAmt Column in Descending order and then Rank each record within the Loan Group accordingly. Have a Play with this:-
vb.net Code:
Private myData As New DataTable Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'Start by Grouping the Different Loan Types Dim loanTypes As IEnumerable(Of IGrouping(Of String, DataRow)) = myData.Rows.Cast(Of DataRow).GroupBy(Function(x) x(0).ToString) Dim rankValue As Integer 'Iterate through Each Loan Type and Reset the Rank Value at the Start of Each Type For Each currentType As IGrouping(Of String, DataRow) In loanTypes rankValue = 0 'Loop through Each of the Loan Records in the Order Required, Increment the Rank Value and Set the Rnk Value For Each loanRecord As DataRow In currentType.OrderByDescending(Function(x) CDec(x(1))) rankValue += 1 loanRecord(2) = rankValue Next Next 'Re-Order the DataTable in Type and Rank Order myData.DefaultView.Sort = ("Type ASC, Rnk ASC") End Sub
All you need to do now is add the DataTable to a suitable control to display the results.
Hope that helps.
Cheers,
Ian
Last edited by IanRyder; Aug 23rd, 2015 at 05:43 AM.
Reason: Noticed Numeric Type Mistake
-
Nov 7th, 2017, 07:33 PM
#5
Junior Member
Re: Partition and Rank datatable by a certain column
Originally Posted by IanRyder
Hi,
From what I understand of your question, I would use a combination of LINQ and traditional For Loops here to make things easier to read and understand.
In your case you need to Group your DataRows by the Type column, Order the LoanAmt Column in Descending order and then Rank each record within the Loan Group accordingly. Have a Play with this:-
vb.net Code:
Private myData As New DataTable
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Start by Grouping the Different Loan Types
Dim loanTypes As IEnumerable(Of IGrouping(Of String, DataRow)) = myData.Rows.Cast(Of DataRow).GroupBy(Function(x) x(0).ToString)
Dim rankValue As Integer
'Iterate through Each Loan Type and Reset the Rank Value at the Start of Each Type
For Each currentType As IGrouping(Of String, DataRow) In loanTypes
rankValue = 0
'Loop through Each of the Loan Records in the Order Required, Increment the Rank Value and Set the Rnk Value
For Each loanRecord As DataRow In currentType.OrderByDescending(Function(x) CDec(x(1)))
rankValue += 1
loanRecord(2) = rankValue
Next
Next
'Re-Order the DataTable in Type and Rank Order
myData.DefaultView.Sort = ("Type ASC, Rnk ASC")
End Sub
All you need to do now is add the DataTable to a suitable control to display the results.
Hope that helps.
Cheers,
Ian
Sir am a beginner
how can I add the DataTable to a suitable control to display the results. Please.
-
Nov 7th, 2017, 07:43 PM
#6
Re: [RESOLVED] Partition and Rank datatable by a certain column
Use a DataGridView. To bind it...
DataGridView1.DataSource = myData.DefaultView
In future, please start your own thread. When you hijack a resolved thread, it is revived and everyone who originally answered is notified. It's not good forum etiquette...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|