首页 > 解决方案 > 当数字分开时对数字进行排序

问题描述

我的数据网格中有一个列显示了一些数字。由于这些数字太大,为了更容易阅读,我需要分隔它们的数字。(1985318 -> 1,985,318)我使用以下代码执行此操作:

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);

显然,我必须将这些数字的数据类型分配给"LongText"in MS Access(因为“1,985,318”不是数字)。但问题是,由于它们被定义为字符串而不是数字,所以当我尝试对它们进行排序时,它们没有正确排序。我想我不能同时将他们的数字分开并正确排序。

您对如何完成有任何建议吗?

标签: c#sortinggridviewtypesdigits

解决方案


所以正如 RobertBaron 提到的,我使用字符串格式解决了这个问题。我在 Grid_CellFormatting EventHandler 中使用了以下代码:

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

推荐阅读