简体   繁体   English

用户键入时,ColumnGridView中的千位分隔符带有逗号

[英]Thousand Separator with comma in columnGridView when the user is typing

I am gonna to separate numbers with comma, What I am seeking is exactly like this 我要用逗号分隔数字,我想要的就是这样

Comma Separator in c# C#中的逗号分隔符

but the solution in that question did not work for me as I am doing this in column in gridview instead of textbox. 但是该问题的解决方案对我不起作用,因为我正在gridview的列而不是文本框中执行此操作。

What I have done till now (part of the class for columns in a gridview): 到目前为止,我所做的事情(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 当我在列中输入12345时---> 51,234

Pleae if my sayings are not clear tell me and I will explain it more 如果我的说法不清楚,请告诉我,我会进一步解释

The problem is that when you change the textbox text, the caret goes to the first position of the TextBox . 问题在于,当您更改文本框文本时,插入标记会移至TextBox的第一个位置。 So after setting the TextBox text with the formatted text, you must add this line to go to the end: 因此,在将TextBox文本设置为带格式的文本之后,必须添加以下行才能结束:

base.SelectionStart = base.Text.Length; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM