I have a text datagridviewcolumn displaying an integer.
I set the format property to colTextDefaultCellStyle.Format = "#,##0";
.
So the number that is displayed gets a thousand separator.
When in edit mode I do not want to show the thousand separator.
But I cannot figure out how to do it. This e.g. doesn't work:
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";
}
}
You need to apply the actual formatting and then set e.FormattingApplied
to 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;
}
}