vb.net - .net - Sorting a Data Table -
how sort datatable using vb.net? i've read thread having same output. sorting data table how sort datatable
also, i've tried sample in microsoft still it's not sorting.
dim locationtable new datatable("location") ' add 2 columns locationtable.columns.add("state") locationtable.columns.add("zipcode") ' add data locationtable.rows.add("washington", "1") locationtable.rows.add("california", "2") locationtable.rows.add("hawaii", "3") locationtable.rows.add("hawaii", "4") locationtable.rows.add("hawaii", "5") locationtable.rows.add("hawaii", "6") locationtable.rows.add("hawaii", "7") locationtable.rows.add("hawaii", "8") locationtable.rows.add("hawaii", "9") locationtable.rows.add("hawaii", "10") locationtable.acceptchanges() console.writeline("rows in original order" & vblf & " state " & vbtab & vbtab & " zipcode") each row datarow in locationtable.rows console.writeline(" {0} " & vbtab & " {1}", row("state"), row("zipcode")) next ' create dataview dim view new dataview(locationtable) ' sort state , zipcode column in descending order view.sort = "zipcode asc" console.writeline(vblf & "rows in sorted order" & vblf & " state " & vbtab & vbtab & " zipcode") each row datarowview in view console.writeline(" {0} " & vbtab & " {1}", row("state"), row("zipcode")) next
the output rows in sorted order
state zipcode
washington 1
hawaii 10
california 2
hawaii 3
hawaii 4
hawaii 5
hawaii 6
hawaii 7
hawaii 8
hawaii 9
it should 1 ~ 10 right? :(
your zipcode column considered string, need specify coumn datatype int
try this
dim locationtable new datatable("location") ' add 2 columns locationtable.columns.add("state",type.gettype("system.string")) locationtable.columns.add("zipcode",type.gettype("system.int32")) ' add data locationtable.rows.add("washington", 1) locationtable.rows.add("california", 2) locationtable.rows.add("hawaii", 3) locationtable.rows.add("hawaii", 4) locationtable.rows.add("hawaii", 5) locationtable.rows.add("hawaii", 6) locationtable.rows.add("hawaii", 7) locationtable.rows.add("hawaii", 8) locationtable.rows.add("hawaii", 9) locationtable.rows.add("hawaii", 10) locationtable.acceptchanges() console.writeline("rows in original order" & vblf & " state " & vbtab & vbtab & " zipcode") each row datarow in locationtable.rows console.writeline(" {0} " & vbtab & " {1}", row("state"), row("zipcode")) next ' create dataview dim view new dataview(locationtable) ' sort state , zipcode column in descending order view.sort = "zipcode asc" console.writeline(vblf & "rows in sorted order" & vblf & " state " & vbtab & vbtab & " zipcode") each row datarowview in view console.writeline(" {0} " & vbtab & " {1}", row("state"), row("zipcode")) next
f use dataset...check
if dataset isnot nothing amdalso dataset.tables(0).rows.count>0 // code here end if
Comments
Post a Comment