首页 > 解决方案 > 在 DataGridView 中使用格式

问题描述

我的 DGV 有一些问题,DGV 由一个至少有 30 列的数据表限制(所有期望 PK 和 FK,都设置为字符串),现在有一个特定的列是我想要制作的产品成本它喜欢这个示例:359.99 欧元,每次用户单击该列单元格时,它将显示空值 0.00 欧元,不能在单元格内编辑货币符号,点分隔符也是如此。

所以这就是我的目标,我正在尝试做我在 windows 窗体中提到的所有事情,我已经通过代码尝试过,但是它没有做任何事情。最后我应该采取什么样的步骤来获得预期的结果?

我已经为该列尝试了以下代码,但是正如我之前所说,它什么也没做,虽然我在 StarkOverflow 中发现了一个与我类似的问题,但似乎我应该在绑定到 DGV 之前输入此代码(实际上我没有'不要通过代码绑定它,我想我现在不需要它),但我认为这是不可能的,因为我需要绑定 DGV 以便我可以编辑它(还有一个未绑定的列,包含行的删除按钮,但我认为这并不重要):

            advancedDataGridView1.Columns[14].DefaultCellStyle.Format = "c2";
            advancedDataGridView1.Columns[14].DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("fr-FR");

先感谢您。

标签: c#datagridviewformatcurrency-formatting

解决方案


我不确定“为什么”您要将“COST”值存储为string.Cost 意味着一个数值decimal,并且它“应该”作为 adecimal或其他数值存储在数据库中,而不是作为 astring.这将创建每次string在数字上下文中使用时都会做更多的工作......就像你目前的情况一样。

此外,如果某些用户键入“Hello”作为“Cost”值,这可能会使 DB 处于不一致的状态。有些东西闻起来很腥,这似乎实际上会“制造”问题。

您是否出于某种原因将“成本”值保存为string

将单元格格式设置为“c2”不会做任何事情“当”列“类型”是string.

格式化时,单元格必须是正确的“类型”才能正确格式化。在这种情况下,decimal类型适用于货币值。

另外,应该注意的是,如果您的代码“手动”将列添加到网格中,那么代码行......</p>

dataGridView1.Columns[14].DefaultCellStyle.Format = "c2";
dataGridView1.Columns[14].DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("fr-FR");
 

…在添加这些列之前显然不起作用。

使用DataSource.You FIRST add the时同样适用DataSource,然后代码格式化列。

Again, this all seems very strange to store a decimal value into the DB as a string. I am making this assumption as your code “could” technically convert the decimal value in the DB to a string in the query. If this is the case, then a simple fix is to alter the query to not convert the value to a string.

Below is an example of what is described above. The example uses a DataTable as a DataSource. It has two columns such that the first column is a decimal type and the second column is of a string type. The table has some rows added such that each row has the “same” value in each column.

enter image description here

When the formatting is applied after the grid’s DataSource has been set, it should be clear, that the formatting is being “ignored” on the cells that are of string type and ARE applied to cells of the decimal type.

Since the culture info is set to “fr-FR”, then the “decimal point” is NOT used. Therefore when the user types in numbers, the user needs to use a “comma” where they want the decimal place to be.

For a complete example, drop a DataGridView onto a form and use the posted code below to test what is described above. I hope this makes sense.

private void Form1_Load(object sender, EventArgs e) {
  dataGridView1.DataSource = GetTable();
  dataGridView1.Columns[0].DefaultCellStyle.Format = "c2";
  dataGridView1.Columns[0].DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("fr-FR");
  dataGridView1.Columns[1].DefaultCellStyle.Format = "c2";
  dataGridView1.Columns[1].DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("fr-FR");
}

private DataTable GetTable() {
  DataTable dt = new DataTable();
  dt.Columns.Add("Col0", typeof(decimal));
  dt.Columns.Add("Col1", typeof(string));
  dt.Rows.Add(12.345, 12.345);
  dt.Rows.Add(1.35, 1.35);
  dt.Rows.Add(112.555, 112.555);
  dt.Rows.Add(12.333, 12.333);
  return dt;
}

private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) {
  Debug.WriteLine("Error at Row: " + e.RowIndex + " Col: " + e.ColumnIndex);
  Debug.WriteLine("Error is:" + e.Exception.Message);
  Debug.WriteLine("Value is:" + dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue);
}

推荐阅读