Search code examples
c#sortinggridviewtypesdigits

sorting numbers when their digits are separated


There is a column in my datagrid which shows some numbers. Since these numbers are too large, I need to seperate their digits for the sake of reading them easier.(1985318 -> 1,985,318) I do this by using this code:

int value = (int) Convert.ToInt64(GridView.Rows[i].Cells[j].Value);
string seperated = value.ToString("N1", CultureInfo.InvariantCulture);
GridView.Rows[i].Cells[j].Value = seperated.Remove(seperated.Length - 2);

Obviously I had to assign the datatype of these numbers to "LongText" in MS Access (Because "1,985,318" is not a number). But the problem is that since they are defined as strings and not numbers, when I try to sort them they do not become sorted properly. I thought I cannot simultaneously have their digits separated and sort them properly.

Do you have any suggestions on how can it be done?


Solution

  • So as RobertBaron mentioned, I solved the problem using String formatting. I used the following code in Grid_CellFormatting EventHandler:

    private void Grid_CellFormatting(object sender, CellFormattingEventArgs e)
    {
            GridViewDecimalColumn myCol = Grid.Columns[2] as GridViewDecimalColumn;
            myCol.FormatString = "{0:###,###,###,###,###,###,###}";
    }