1

我有一个显示整数的文本数据网格视图列。
我将格式属性设置为colTextDefaultCellStyle.Format = "#,##0";.

所以显示的数字有千位分隔符。

在编辑模式下,我不想显示千位分隔符。
但我不知道该怎么做。这例如不起作用:

void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];

    if (e.ColumnIndex == colText.Index)
    {
        if (cell.IsInEditMode) cell.Style.Format = "";
        else cell.Style.Format = "#,##0";
    }
}
4

1 回答 1

3

您需要应用实际格式,然后设置e.FormattingApplied为 True。

void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == colText.Index)
    {
        DataGridViewCell cell = dgv.Item[e.ColumnIndex, e.RowIndex];

        if (cell.IsInEditMode)
        {
            e.Value = e.Value.ToString();
        }
        else
        {
            e.Value = e.Value.ToString("#,##0");
        }
        e.FormattingApplied = true;
    }
}
于 2014-10-10T19:47:49.023 回答