[英]Datagridview textbox leave event
I have a function that formats a textbox value to currency. 我有一个函数将文本框值格式化为货币。 I did this in Textbox Leave event.
我在Textbox Leave事件中这样做了。
private void txtSellingPrice_Leave(object sender, EventArgs e)
{
txtSellingPrice.Text = FormatCurrency(txtSellingPrice.Text);
}
Example a user enters 100, the output will be $100. 用户输入100的示例,输出将为100美元。
My question is how will I do it in datagridview cell? 我的问题是我将如何在datagridview单元格中执行此操作? i already tried adding leave event in editing control.
我已经尝试在编辑控件中添加leave事件。 I also tried DefaultCellStyle.Format = "C2", this works but when the user change the current value, say $100.00 change to 50. The output should be $50.00.
我也尝试过DefaultCellStyle.Format =“C2”,这样可行,但是当用户更改当前值时,请将$ 100.00更改为50.输出应为$ 50.00。 Thanks.
谢谢。
The easy method would be assigning a BindingSource to your DataGridView and a BindingList to your BindingSoure. 简单的方法是将BindingSource分配给DataGridView,将BindingList分配给BindingSoure。 Set the default cell style format of the column that displays the currency to C2.
将显示货币的列的默认单元格样式设置为C2。
If you've done that, any added / changed / removed cell of your currency column will automatically be formatted. 如果您已完成此操作,则会自动格式化您的货币列的任何已添加/已更改/已删除的单元格。
However, if you don't want to use the BindingSource, you'll have to do the formatting yourself. 但是,如果您不想使用BindingSource,则必须自己进行格式化。 Use events DataGridViewCell.CellValidating and DataGridViewCell.CellFormating.
使用事件DataGridViewCell.CellValidating和DataGridViewCell.CellFormating。
Let's assume you have a columnCurrency, with a decimal value that should be dislayed in format C2. 假设您有一个columnCurrency,其十进制值应以格式C2显示。 The DefaultCellFormat is set to C2.
DefaultCellFormat设置为C2。
Upon validating the cell, check if the value is really a decimal: 验证单元格后,检查该值是否真的是小数:
private void OnCellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.RowIndex == -1) return;
if (e.ColumnIndex == this.columnValue.Index)
{
decimal value;
e.Cancel = !Decimal.TryParse((string)e.FormattedValue, out value);
}
}
Whenever the cell must be formatted, format the value in e.Value: 每当必须格式化单元格时,请格式化e.Value中的值:
private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == this.columnValue.Index)
{
if (e.Value == null)
{ // show the Null value:
e.Value = e.CellStyle.NullValue;
e.FormattingApplied = true;
}
else
{ // because validated I know value is a decimal:
decimal value = Decimal.Parse((string)e.Value);
e.Value = value.ToString(e.CellStyle.Format);
e.FormattingApplied = true;
}
// If desired apply other formatting, like background colouring etc
}
}
If you don't want to use "C2" as currency format, and prefer to use your function FormatCurrency
you'll have to create a FormatProvider that uses FormatCurrency
to Format and set the FormatProvider of the DefaultCellStyle 如果您不想使用“C2”作为货币格式,并且更喜欢使用您的函数
FormatCurrency
,则必须创建一个FormatProvider,它使用FormatCurrency
格式化并设置DefaultCellStyle的FormatProvider
private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex == this.columnValue.Index)
{
... (check for null value as described above)
else
{
e.Value = String.Format(e.Value, e.CellStyle.FormatProvider);
e.FormattingApplied = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.