carlnelson.com |
|||||
|
|
Problem: The client has a field of data that is a SQL varchar. The data needs to be treated as a numeric for numeric values and string for string values. The data is displayed in a DataGrid control and may be sorted by clicking the column header. Natively, the data becomes a string when retrieved into a System.Data.DataTable. The sort order provided does a string comparison. If the data set is ('20', '21', '200'), the sort of the data as a string will return it in the order 20, 200, 21. The desired sort order is the numeric, 20, 21, 200. Solution: A class was created called QuasiNumber: public class QuasiNumber : IComparable, IConvertible that can hold either integer or string data. The class constructor takes care of determining whether the data is integer or string. The implementation of IComparable requires a CompareTo method of the class: public int CompareTo(object x) } The data is then retrieved from the database as a System.Data.DataTable. A new DataColumn with data type QuasiNumber is created, and populated from values in the data column. SqlDataAdapter DataAdapter = new SqlDataAdapter(cmdSQL); //cmdSQL is a SqlCommand Now, a click on the column header results in a call to the CompareTo method of the IComparable interface of QuasiNumber, resulting in the desired sort order.
|
|
|