问题
I am gonna to separate numbers with comma, What I am seeking is exactly like this
Comma Separator in c#
but the solution in that question did not work for me as I am doing this in column in gridview instead of textbox.
What I have done till now (part of the class for columns in a 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;
}
So what happen with this piece of code:
when I am typing 12345 in column it becomes ---> 51,234
Pleae if my sayings are not clear tell me and I will explain it more
回答1:
The problem is that when you change the textbox text, the caret goes to the first position of the TextBox. So after setting the TextBox text with the formatted text, you must add this line to go to the end:
base.SelectionStart = base.Text.Length;
来源:https://stackoverflow.com/questions/40978277/thousand-separator-with-comma-in-columngridview-when-the-user-is-typing