【问题标题】:sorting numbers when their digits are separated当数字分开时对数字进行排序
【发布时间】:2019-08-18 18:09:02
【问题描述】:

我的数据网格中有一个列显示了一些数字。由于这些数字太大,为了便于阅读,我需要将它们的数字分开。(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);

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

您对如何完成有什么建议吗?

【问题讨论】:

  • 你应该设置显示格式而不是自己格式化。这样,基础值将保持数字并且它们将正确排序。见Set Display format for winform DataGridView
  • @RobertBaron 感谢您的回复。我设法解决了字符串格式的问题。

标签: c# sorting gridview types digits


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 2015-10-14
    相关资源
    最近更新 更多