Sorting numerically in a DataGridViewTextBoxColumn
If you are using a DataTable
then you have to set the DataType
on the DataColumn
. Setting ValueType
on the DataGridViewTextBoxColumn
won't help.
You can set it when creating it:
table.Columns.Add("Number", typeof(int));
You can handle the event SortCompare
to change how the sorting is done, like this:
private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e) {
//Suppose your interested column has index 1
if (e.Column.Index == 1){
e.SortResult = int.Parse(e.CellValue1.ToString()).CompareTo(int.Parse(e.CellValue2.ToString()));
e.Handled = true;//pass by the default sorting
}
}
NOTE: The above code supposes your cell values are convertible to int.
You said your DataGridView
doesn't have DataSource
assigned, that means you Add
the rows manually, so I think you should use numeric
values instead of string
for your cells. That would make the sorting work as you want.