用户键入时在 GridView 列中使用逗号分隔的千位分隔符

Thousand Separator with comma in columnGridView when the user is typing

我要用逗号分隔数字, 我要找的正是这样

但是那个问题的解决方案对我不起作用,因为我是在 gridview 的列而不是文本框中这样做的。

到目前为止我所做的(class 的一部分,用于 gridview 中的列):

public override string Text
{
    get { }
    set { base.Text = GetFormattedText(value); }
}

protected override void OnTextChanged(System.EventArgs e)
{
    base.OnTextChanged(e);
    Text = GetFormattedText(Text);
}

protected virtual string GetFormattedText(string text)
{
    string strText = text.Replace(",", "");
    decimal decValue = System.Convert.ToDecimal(strText);
    strText = decValue.ToString("#,##0");
    return strText;
}

那么这段代码会发生什么:

当我在列中输入 12345 时,它变为 ---> 51,234

如果我的话不清楚请告诉我,我会解释更多

问题是当您更改文本框文本时,插入符号会转到 TextBox 的第一个位置。所以在用格式化文本设置 TextBox 文本后,你必须添加这一行到最后:

base.SelectionStart = base.Text.Length;