c# - Sort a DataTable on converted value of a column -


i working on asp.net app in c#. need sort rows in datatable on converted value of column called 'price', unknown reason, string type column in datatable.

the code snippet below give me sorted datatable on string value of price column, looking sorting on decimal value of price column.

question: expression need use second parameter of select method in code snippet? right now, parameter has value of price asc.

datatable dt = getdata(); dt = dt.select( null, "price asc", dataviewrowstate.currentrows).copytodatatable(); 

update 1

i able make minimal change in above code-snippet , yet use decimal value of price sort on. used convert function convert string value of price column decimal type. function supported in datatable expressions. details of function can found @ location: https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=vs.110).aspx

the code works below.

datatable dt = getdata(); //define expression column decimal version of price dt1.columns("price1").expression = "convert(price, 'system.decimal')" dt = dt.select( null, "price1 asc", dataviewrowstate.currentrows).copytodatatable(); 

convert function example converting value in column called 'total' decimal typeis below. second parameter convert function must name of .net type want convert , should enclosed in single quotes.

mydatacolumn.expression="convert(total, 'system.int32')" 

my best idea convert values decimal, order them , copy them table:

dt = dt.rows.cast<datarow>().orderby(row => convert.todecimal(row["price"])).copytodatatable(); 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -