如何在System.Windows.Forms.DataGrid中设置包含double,float或decimal数字的数据列的精度?
对于DataGridView,例如有How to format a column with number decimal with max and min in DataGridView?。
我希望0.0100000001
显示为0.01
,例如,小数点后的2位精度。我想避免它们看起来像这样,浮动和双重使用科学记数法:
我用来填表的代码是:
var table = new DataTable();
table.Columns.Add("double");
table.Columns.Add("float");
table.Columns.Add("decimal");
table.Columns[0].DataType = typeof(double);
table.Columns[1].DataType = typeof(float);
table.Columns[2].DataType = typeof(decimal);
table.Rows.Add(new object[] { 0.00000000000423, 0.00000000000423, 0.00000000000423 });
dataGrid1.DataSource = table;
注意:我知道DataGrid已经过时,但是我处理遗留代码,请不要发表评论告诉我使用DataGridView - 它对我没有帮助。
答案 0 :(得分:2)
我从@stuartd评论中获得了我的解决方案。我需要为DataGrid设置Format
列of the current table style。
/// <summary>
/// Getting and setting the column widths of a DataGrid is not easy at all.
/// This helper class handles it, including saving and restoring from a string.
/// </summary>
static class DataGridColumnWidthExtensions
{
/// Get the current table style.
public static DataGridTableStyle GetCurrentTableStyle(this DataGrid grid)
{
// DataGrid holds the current grid table style into a private field called myGridTable.
// The field points to the "default" table style even if TableStyles is empty. The
// default table style is also private/internal.
// See https://stackoverflow.com/a/39832554/492336 and
// https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/DataGrid.cs,211.
FieldInfo[] fields = grid.GetType().GetFields(
BindingFlags.NonPublic |
BindingFlags.Instance);
return (DataGridTableStyle)fields.First(item => item.Name == "myGridTable").GetValue(grid);
}
}
然后我们可以迭代GridColumnStyles并为每个数字列设置Format属性:
var tableStyle = dataGrid1.GetCurrentTableStyle();
for (int ii = 0; ii < table.Columns.Count; ii++)
{
var columnStyle = tableStyle.GridColumnStyles[ii] as DataGridTextBoxColumn;
if (columnStyle == null)
{
// DataGridTextBoxColumn inherits DataGridColumnStyle but in theory
// a column might be of some other type deriving from DataGridColumnStyle.
continue;
}
var columnType = table.Columns[ii].DataType;
if (columnType != typeof(double) && columnType != typeof(float) && columnType != typeof(decimal))
{
// We set the format only for numeric columns.
continue;
}
// 2 digits after the decimal mark.
columnStyle.Format = "N2";
}
答案 1 :(得分:0)
请找到更新的ans
function RedirectPage(path)
{
var intWidth = screen.width - 10; //Adjust for the end of screen var intHeight = screen.height - 80; //Adjust for the Icon Bar at the bottom of the window.
var strWinProp = " toolbar=no" //Back, Forward, etc...
+ ",location=no" //URL field
+ ",directories=no" //"What's New", etc...
+ ",status=yes" //Status Bar at bottom of window.
+ ",menubar=no" //Menubar at top of window.
+ ",resizable=yes" //Allow resizing by dragging.
+ ",scrollbars=yes" //Displays scrollbars is document is larger than window.
+ ",titlebar=yes" //Enable/Disable titlebar resize capability.
+ ",width="+intWidth //Standard 640,800/788, 800/788
+ ",height="+intHeight //Standard 480,600/541, 600/566
+ ",top=0" //Offset of windows top edge from screen.
+ ",left=0" //Offset of windows left edge from screen.
+ "";
var win = window.open(path,'_blank',strWinProp);
window.location.href ="closer.html";
始终首先绑定datagrid,然后格式化列