将数据源的数据格式化显示,加上金额符号
2014-12-11 19:34
246 查看
将数据源的数据格式化显示,加上金额符号
例如,将textbox内的数值格式化加上货币符号,在textbox的内容改变时,textbox绑定的数据源的值的格式保持不变,还是数值,不会有货币符号。
创建一个Binding,向Parse事件和Format事件添加ConvertEventHandler委托,并通过DataBindings属性向TextBox控件的BindingsCollection添加Binding添加到 Format 事件的 DecimalToCurrencyString 事件委托使用 ToString 方法将绑定值(Decimal 类型)格式化为货币类型。 添加到 Parse 事件的 CurrencyStringToDecimal 事件委托将控件所显示的值转换回 Decimal 类型。
实现的代码如下:
private void MakeSet()
{
// Create a DataSet.
ds = new DataSet("myDataSet");
// Create DataTable.
DataTable tOrders = new DataTable("Orders");
// Create three columns, and add them to the table.
DataColumn cOrderAmount =
new DataColumn("OrderAmount", typeof(decimal));
tOrders.Columns.Add(cOrderAmount);
// Add the tables to the DataSet.
ds.Tables.Add(tOrders);
/* Populate the tables. For each customer and order,
create two DataRow variables. */
DataRow newRow1;
newRow1 = tOrders.NewRow();
newRow1["OrderAmount"] = Convert.ToDecimal(textBox2.Text.ToString());
tOrders.Rows.Add(newRow1);
}
protected void BindControls()
{
textBox1.DataBindings.Clear();
// Creates the binding first. The OrderAmount is a Decimal type.
Binding b = new Binding
("Text", ds, "Orders.OrderAmount");
// Add the delegates to the event.
b.Format += new ConvertEventHandler(DecimalToCurrencyString);
b.Parse += new ConvertEventHandler(CurrencyStringToDecimal);
textBox1.DataBindings.Add(b);
}
private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
{
// The method converts only to string type. Test this using the DesiredType.
if (cevent.DesiredType != typeof(string)) return;
// Use the ToString method to format the value as currency ("c").
cevent.Value = ((decimal)cevent.Value).ToString("c");
}
private void CurrencyStringToDecimal(object sender, ConvertEventArgs cevent)
{
// The method converts back to decimal type only.
if (cevent.DesiredType != typeof(decimal)) return;
// Converts the string back to decimal using the static Parse method.
cevent.Value = Decimal.Parse(cevent.Value.ToString(),
NumberStyles.Currency, null);
}
参考链接:
# Binding.Parse 事件
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.binding.parse.aspx
将数据源的数据格式化显示,加上金额符号
例如,将textbox内的数值格式化加上货币符号,在textbox的内容改变时,textbox绑定的数据源的值的格式保持不变,还是数值,不会有货币符号。
创建一个Binding,向Parse事件和Format事件添加ConvertEventHandler委托,并通过DataBindings属性向TextBox控件的BindingsCollection添加Binding添加到 Format 事件的 DecimalToCurrencyString 事件委托使用 ToString 方法将绑定值(Decimal 类型)格式化为货币类型。 添加到 Parse 事件的 CurrencyStringToDecimal 事件委托将控件所显示的值转换回 Decimal 类型。
实现的代码如下:
private void MakeSet()
{
// Create a DataSet.
ds = new DataSet("myDataSet");
// Create DataTable.
DataTable tOrders = new DataTable("Orders");
// Create three columns, and add them to the table.
DataColumn cOrderAmount =
new DataColumn("OrderAmount", typeof(decimal));
tOrders.Columns.Add(cOrderAmount);
// Add the tables to the DataSet.
ds.Tables.Add(tOrders);
/* Populate the tables. For each customer and order,
create two DataRow variables. */
DataRow newRow1;
newRow1 = tOrders.NewRow();
newRow1["OrderAmount"] = Convert.ToDecimal(textBox2.Text.ToString());
tOrders.Rows.Add(newRow1);
}
protected void BindControls()
{
textBox1.DataBindings.Clear();
// Creates the binding first. The OrderAmount is a Decimal type.
Binding b = new Binding
("Text", ds, "Orders.OrderAmount");
// Add the delegates to the event.
b.Format += new ConvertEventHandler(DecimalToCurrencyString);
b.Parse += new ConvertEventHandler(CurrencyStringToDecimal);
textBox1.DataBindings.Add(b);
}
private void DecimalToCurrencyString(object sender, ConvertEventArgs cevent)
{
// The method converts only to string type. Test this using the DesiredType.
if (cevent.DesiredType != typeof(string)) return;
// Use the ToString method to format the value as currency ("c").
cevent.Value = ((decimal)cevent.Value).ToString("c");
}
private void CurrencyStringToDecimal(object sender, ConvertEventArgs cevent)
{
// The method converts back to decimal type only.
if (cevent.DesiredType != typeof(decimal)) return;
// Converts the string back to decimal using the static Parse method.
cevent.Value = Decimal.Parse(cevent.Value.ToString(),
NumberStyles.Currency, null);
}
参考链接:
# Binding.Parse 事件
http://msdn.microsoft.com/zh-cn/library/system.windows.forms.binding.parse.aspx
相关文章推荐
- react native输入框 输入金额等数据后如何格式化显示
- ABAP--如何进行sap的金额数据存储和显示之间的转换(CURRENCY_AMOUNT_DISPLAY_TO_SAP)
- ABAP--如何进行sap的金额数据存储和显示之间的转换(CURRENCY_AMOUNT_DISPLAY_TO_SAP)
- 控件(七)——GridView控件以SqlDataSource控件为数据源显示数据
- 过滤数据源,只显示那些符合条件的数据
- (转)jqgrid 显示格式化数据
- ListView Column Item绑定不同控件和数据格式化显示
- jsData 使用教程(二) 对显示的数据进行格式化
- 过滤数据源,只显示那些符合条件的数据--Query
- 数据格式化显示
- ABAP--如何进行sap的金额数据存储和显示之间的转换(CURRENCY_AMOUNT_DISPLAY_TO_SAP)
- 于GridView的数据格式化显示
- ABAP--如何进行sap的金额数据存储和显示之间的转换(CURRENCY_AMOUNT_DISPLAY_TO_SAP)
- 如何让GridView控件在绑定的数据源没有数据时也显示标题
- jqgrid 显示格式化数据
- [borland eco 技术]experssionhandle数据源,在datagrid中利用checkbox显示并编辑boolean类型数据
- 显示数据源中的数据
- AdvancedDataGrid 用XML做数据源 显示树形数据
- 如何让GridView控件在绑定的数据源没有数据时也显示标题
- GridView数据绑定时数据源为空时显示表头和"没有记录"