Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

sql - Sorting currency in VB.NET

I'm having difficulty addressing the issue of a currency sorting problem within our in-house software package. The software in question is written using VB.NET and the values are contained within DataGridView, the relevant code is: SQLReader.Read() Code

and the result is: Value Column incorrectly sorted

There are more examples further down the column where values with commas inside them don't appear to sort correctly as well. How do I get them to sort?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I see your code is converting numeric currency values to a string and then displaying the string in the DataGridView - that's your problem: this means the DataGridView will be performing lexicographic-ordering on the strings instead of numerical ordering (i.e. "21" is ordered after "123" because '2' > '1'.

I don't know what the definition of m_DataRow is, but it smells like you're using untyped DataSets, or perhaps a crudely created typed-DataSet but without any meaningful column names and types.

Unfortunately because your DataGridView looks to be bound to this m_dataRow object you're stuck unless you can recreate it correctly (e.g. with actual column names and types). However if you're using untyped DataSets then just change your code to stop formatting currency values to string but instead provide the raw Decimal value directly, and then use DataGridView's own column formatting to specify the "C" (for currency) format string.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...