用户输入时在ColumnGridView中以逗号分隔的千分隔符 [英] Thousand Separator with comma in columnGridView when the user is typing

查看:264
本文介绍了用户输入时在ColumnGridView中以逗号分隔的千分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将用逗号分隔数字,
我正在寻找的就是这样

c#中的逗号分隔符



但是这个问题的解决方案并不适用于我,因为我在gridview而不是文本框的列中执行此操作。



我有什么直到现在(gridview中的列的一部分):

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

保护覆盖无效OnTextChanged(System.EventArgs e)
{
base.OnTextChanged(e);
Text = GetFormattedText(Text);


保护虚拟字符串GetFormattedText(字符串文本)
{
字符串strText = text.Replace(,,);
decimal decValue = System.Convert.ToDecimal(strText);
strText = decValue.ToString(#,## 0);
返回strText;
}

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



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



如果我的说法不是清楚地告诉我,我会更详细地解释它

解决方案

问题是,当您更改文本框文本时,插入符号将转到 TextBox 的第一个位置。因此,在设置带有格式文本的 TextBox 文本后,您必须添加以下代码行才能结束:

  base.SelectionStart = base.Text.Length; 


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

解决方案

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; 

这篇关于用户输入时在ColumnGridView中以逗号分隔的千分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆