简体   繁体   English

.Net C#WinForm DataGridView-文本框单元格:将数字用户输入更新/更改为特定区域性货币的最佳方法是什么?

[英].Net C# WinForm DataGridView - TextBox Cell: what's the best way to update/change from a numeric user input to a specific culture currency?

A. BACKGROUND OF THE QUESTION A.问题的背景

I am trying to have a cell in a datagridview to accept a numeric only user input, which I've handled on the Grid's EditingControlShowing event and the textbox cell's KeyPress event. 我试图在datagridview中有一个单元格来接受仅数字用户输入,这是我在Grid的EditingControlShowing事件和文本框单元格的KeyPress事件中处理的。 Now, after the user hits enter or leaves the focus from that cell, I would like to update the cell's value to have a decimal separator and the specific culture currency. 现在,在用户点击该单元格之后,进入或离开焦点,我想更新该单元格的值以使用小数点分隔符和特定的区域性货币。 The following is the flow that I'm trying to explain: 以下是我要解释的流程:

  1. numeric user input on a datagridview cell -> done (NOTE: cell's ValueType == int || double already). 在datagridview单元上的数字用户输入->完成(注意:单元的ValueType == int ||已经是double了)。

  2. on cell's leave focus, update the cell value to have decimal separator and specific currency symbol from a culture, to display. 在单元格的离开焦点上,更新单元格值以显示小数点分隔符和区域性中的特定货币符号。 (The reason to have a specific culture so that if the application is installed on a different machine with different culture, it will stay on that specific culture). (具有特定区域性的原因,这样,如果将应用程序安装在具有不同区域性的其他计算机上,它将保留在该特定区域性上)。

  3. on cell's enter focus or editing, i would like to strip all of that separators and currency symbol and go back to the numeric only user input value. 关于单元格的输入焦点或编辑,我想剥离所有的分隔符和货币符号,然后返回仅数字的用户输入值。

B. THE QUESTIONS B.问题

  1. First of all, is there a way to implement this without using the CellFormatting event? 首先,有没有一种方法可以在不使用CellFormatting事件的情况下实现呢? (I would like to prefer to avoid using this event because this event is called so often and I don't want to put burden on the performance already). (我希望避免使用此事件,因为经常调用此事件,并且我不想给性能造成负担)。

  2. I already use the following: 我已经使用以下内容:

     NumberFormatInfo numberFormat = CultureInfo.CreateSpecificCulture("de-DE").NumberFormat; double d = double.Parse(<value>, numberFormat); string test = d.ToString(numberFormat); 

But, it doesn't really updates the value, what do I miss here? 但是,它并没有真正更新价值,在这里我想念什么?

  1. what's the best way to implement the behavior, for example, is the cellValueChanged event and CellEnter event will be the best events to handle the above behavior? 实现该行为的最佳方法是什么,例如,cellValueChanged事件和CellEnter事件是否是处理上述行为的最佳事件? or CellValidated and CellEnter events will be best events? 还是CellValidated和CellEnter事件将是最好的事件?

Basically, I'm confused on which event is the best event to handle the behavior (without being called so often such as CellFormatting event which is called every time the grid is painted or basically every time you touch the grid), and how to enforce the NumberFormatInfo to that string value. 基本上,我对哪个事件是处理该行为的最佳事件感到困惑(没有这么频繁地调用,例如CellFormatting事件,该事件在每次绘制网格时或基本上在每次触摸网格时都被调用),以及如何执行将NumberFormatInfo更改为该字符串值。

Thanks! 谢谢!

EDIT (15/09/2016): I actually can update the value to what I want, by doing this: 编辑(15/09/2016):通过执行以下操作,我实际上可以将值更新为所需的值:

string test = d.ToString("c2", numberFormat);

I thought the numberFormat itself is sufficient to applying the format because it has the information, but I actually have to explicitly specifying the format such as "c2" from above example, and it acts like a "command" which take the information from a "dictionary" which in this case the "numberFormat" (the definition). 我认为numberFormat本身就足以应​​用格式,因为它具有信息,但是实际上我必须从上述示例中明确指定格式,例如“ c2”,并且它的作用类似于“命令”,它从“字典”,在这种情况下为“ numberFormat”(定义)。

So, this is how it works for me: 因此,这对我来说是这样的:

  1. I added DataGridViewColumn programmatically, so when I create those columns, I setup these properties: 我以编程方式添加了DataGridViewColumn ,因此在创建这些列时,我设置了以下属性:

     DataGridViewTextBox column = new DataGridViewTextBoxColumn(); ... column.ValueType = typeof(decimal); column.DefaultCellStyle.Format = "c2"; column.DefaultCellStyle.FormatProvider = CultureInfo.CreateSpecificCulture("de-DE").NumberFormat; 
  2. Handle on the DataGridView EditingControlShowing event, to remove the Currency Symbol and to only shows the number only during editing the cell's value: 处理DataGridView EditingControlShowing事件,以删除“货币符号”并仅在编辑单元格的值期间显示数字:

     public void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ... if(decimal.TryParse(grid.EditingControl.Text, NumberStyles.Currency, CultureInfo.CreateSpecificCulture("de-DE").NumberFormat, out decimalValue)) { grid.EditingControl.Text = decimalValue.ToString("#", CultureInfo.CreateSpecificCulture("de-DE").NumberFormat); } ... } 
  3. Handle on the `CellValidating' event, to make sure the user input value is in proper format (or in numeric only): 处理“ CellValidating”事件,以确保用户输入值的格式正确(或仅数字形式):

     public void Grid_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { ... //No need to check/validate the new row if (gridSender.Rows[e.RowIndex].IsNewRow) { return; } ... if(!decimal.TryParse(value, NumberStyles.Currency, CultureInfo.CreateSpecificCulture("de-DE").NumberFormat, out result)) { e.Cancel = true; } ... } 

NOTE: if you do step #1 above (ie "c2" and specifying the FormatProvider ), then it will automatically shows the Currency Symbol and the decimal points, every time the user leave focus of the cell of that column. 注意:如果您执行上述第1步(即"c2"并指定FormatProvider ),则每次用户离开该列单元格的焦点时,它将自动显示货币符号和小数点。 So, the only thing needs to do, is stripping the currency symbol and how to show the value in numeric only by doing step #2. 因此,唯一要做的就是仅通过执行步骤2来剥离货币符号以及如何以数字显示值。 Step #3, is just another validation to make sure the input is numeric. 步骤#3,只是另一个验证,以确保输入为数字。

Example Flow: 流程示例:

  1. users click on the cell and in edit more, it will display 1000 用户单击该单元格并进行更多编辑,它将显示1000
  2. user click on another cell or focus away, it will display $1.000,00 用户单击另一个单元格或将焦点移开,它将显示$1.000,00
  3. user click on the cell again and in editing, it will display 1000 用户再次单击该单元格并进行编辑,它将显示1000

The above solutions works good enough for me. 上述解决方案对我来说足够好。

Why is having a culture currency symbol such a big deal for you Jay??? 为什么拥有文化货币符号对您来说如此重要呢? Also why are decimal points such a big deal??? 为什么小数点这么大呢??? Can the program work without these and achieve the same goal??? 如果没有这些程序,程序可以工作并达到相同的目标吗??? Would it not be better to determine the culture symbol of the currency at the outset in the program and possibly use it as a variable and append it later to the string? 在程序开始时确定货币的文化符号并可能将其用作变量并将其稍后附加到字符串上会更好吗?

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

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