I’ve got DataGridView bound to a DataTable with some numeric data and I need two things to ensure are working:
- Numeric sorting (
doubles/ints) on every column with numbers, - Possibility to add some text (like ‘x’ or ‘-‘) instead of numbers in special cases.
Is there a simple way I could add them at the level of DataTable?
I know I can change type of DataTable‘s columns to String, but then I’d need to add much more code just for the sake of sorting columns by already built-in way – and I certainly don’t need to change the way glyphs are drawn, etc.
Also, because of amount and way my DGVs are spawned, it won’t be as easy as adding one method for every DGV I’ve got.
[Edit]
I write the same set of DataTables to excel files, so drastic changes to the way they work would be problematic and error prone – I’d like to avoid it.
My code for filling dataTables in most occasions looks like this:
var row = MyDataTable.GetMyDataTableNewRow();
row.partName = part.name;
row.weight = part.weight.ToString(); // it's double
row.quantity = part.GetParentSet().FindQuantities(part.name).ToString(); // it's int
row.maxsize = part.CalculateMaxSize().ToString();
MyDataTable.AddMyDataTableRow(row);
The only way to define you desired behaviour at
DataTablelevel, is introducing a custom type holding both a double and a string, and defining an appropriateTypeConverter(to correctly show the objects and enable editing in theDataGridView).e.g. :
Usage example:
Result: