Thousand Separator with comma in columnGridView when the user is typing

≯℡__Kan透↙ 提交于 2019-12-25 09:16:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!