当前位置: 首页 > 编程语言 > C#

货币文化格式不适用于DataGridView列分享

时间:2023-04-10 22:34:58 C#

C#学习教程:货币文化格式化不适用于DataGridView列我写的代码似乎可以在其中一个运行,但不能在另一个运行。两个DGV都是这样设置的:首先将数据加载到DataTable中。然后将BindingSource链接到此DataTable。最后,DGV将此BindingSource对象用于其数据。我在窗体的加载事件中使用以下代码来自定义两个DGV的货币列:dataGridView.Columns[columnIndexHere].DefaultCellStyle.FormatProvider=CultureInfo.GetCultureInfo("de-DE");dataGridView.Columns[columnIndexHere].DefaultCellStyle。Format=String.Format("c")似乎将一个DGV中的列格式化为另一个DGV。此外,它不工作的DGV在其父表单中甚至没有那么多功能代码。我检查了DGV的代码和属性,看看我是否在其他地方更改了格式,但我不能找到任何东西..两个DGV加载数据的方式之间唯一的微小区别是,对于第一个DGV的数据表(格式化工作的地方),数据是通过SELECT语句(通过我自己的函数)加载的......在第二个DGV的数据表(格式化不起作用),我不从数据库加载任何数据,而只是手动定义列(在数据表中),因为在这种情况下,用户需要输入数据..为什么格式化不起作用的任何线索DGV的第二部作品?编辑:添加更多信息:为了演示这个问题,我创建了一个新的C#winforms项目,只向其中添加了一个DataGridView,并将以下代码添加到Load事件中。即使对于这段代码,也没有货币格式:DataTabledataTable=newDataTable();dataTable.Columns.Add("ColA");dataTable.Columns.Add("ColB");dataTable.Columns.Add("ColC");dataTable.Rows.Add("约翰",832.293,"伦敦");dataTable.Rows.Add("爱丽丝",32972978.20489,"亚特兰大");dataTable.Rows.Add("马克",9184793284739,"东京");BindingSourcebindingSource=newBindingSource();bindingSource.DataSource=数据表;dataGridView1.DataSource=bindingSource;varformat=(NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();format.CurrencySymbol="Mn.";数据网格视图1。["ColB"].DefaultCellStyle.FormatProvider=CultureInfo.GetCultureInfo("de-DE");dataGridView1.Columns["ColB"].DefaultCellStyle.Format=String.Format("c");希望这有助于更多诊断问题..你不需要BindingSource来列出所有行dataGridView1.DataSource=dataTable并且因为你正在使用DataTable删除这部分dataGridView1.Columns[“ColB”]。DefaultCellStyle.Format=String.Format("c");并尝试使用CellFormatting事件。privatevoiddataGridView1_CellFormatting(objectsender,DataGridViewCellFormattingEventArgse){if(e.ColumnIndex==1)//ColumnColB{if(e.Value!=null){e.CellStyle.Format="c";}}}我要了会有帮助:)(之前从问题中回答)定义两列后添加了下面一行现在可以了:大家有用,需要多了解C#学习教程,希望大家多多付出注意—qBDFrmDTForDisplay.Columns["CombinedPrice"].DataType=Type.GetType("System.Decimal");本文收集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处:

猜你喜欢