Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Datagridview格式列问题_C#_Datagridview_Datagridviewcolumn - Fatal编程技术网

C# Datagridview格式列问题

C# Datagridview格式列问题,c#,datagridview,datagridviewcolumn,C#,Datagridview,Datagridviewcolumn,我正在尝试用两种不同的格式格式化两列 第一列4位宽度,当用户输入1234时,我需要输出为123.4 我试着用 __dgw.Columns["column"].DefaultCellStyle.Format = "N1"; 但是输出是1234.0,我不需要逗号,只需要123.4,我试过d1等等 有蒙面柱这样的东西吗 我还需要一种方法来创建另一个带有##-#-#-#掩码的列 提前感谢你的帮助 < P>如果你打算在今后更频繁地使用这种行为,考虑下面的链接以供参考,特别是最后一个: 如果这是

我正在尝试用两种不同的格式格式化两列

第一列4位宽度,当用户输入1234时,我需要输出为123.4

我试着用

__dgw.Columns["column"].DefaultCellStyle.Format = "N1";
但是输出是1234.0,我不需要逗号,只需要123.4,我试过d1等等

有蒙面柱这样的东西吗

我还需要一种方法来创建另一个带有##-#-#-#掩码的列


提前感谢你的帮助

< P>如果你打算在今后更频繁地使用这种行为,考虑下面的链接以供参考,特别是最后一个:

如果这是一笔一次性交易,可能会少做以下工作:

this.dataGridView1.Columns[0].DefaultCellStyle.Format = "000.0";
this.dataGridView1.Columns[1].DefaultCellStyle.Format = "00-00-00";

this.dataGridView1.CellFormatting += this.DataGridView1_CellFormatting;

private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  int value = 0;

  if (e.Value != null && int.TryParse(e.Value.ToString(), out value))
  {
    if (e.ColumnIndex == 0)
    {
      // Must manually move decimal. Setting Format will not do this for you.
      e.Value = (decimal)value / 10;
    }
    else if (e.ColumnIndex == 1)
    {
      // Format won't affect e.Value of type string.
      e.Value = value;
    }
  }
}
这将提供所需的准确结果,同时保留潜在价值

╔════════════╦═══════════╦═══════════════════╗
║ User Input ║ Displayed ║ Actual Cell Value ║
╠════════════╬═══════════╬═══════════════════╣
║    1234    ║   123.4   ║       1234        ║
║   123456   ║  12-34-56 ║      123456       ║
╚════════════╩═══════════╩═══════════════════╝
这还假设您已将用户限制为长度分别为4和6的数字条目,具体操作如下:

this.dataGridView1.EditingControlShowing += this.DataGridView1_EditingControlShowing;

private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
  e.Control.KeyPress -= new KeyPressEventHandler(AllowNumericOnly);
  if (dataGridView1.CurrentCell.ColumnIndex == 0 || dataGridView1.CurrentCell.ColumnIndex == 1)
  {
    TextBox tb = e.Control as TextBox;
    if (tb != null)
    {
      tb.Tag = dataGridView1.CurrentCell.ColumnIndex == 0 ? 4 : 6;
      tb.KeyPress += new KeyPressEventHandler(this.AllowNumericOnly);
    }
  }
}

private void AllowNumericOnly(object sender, KeyPressEventArgs e)
{
  var control = sender as Control;
  int length = (int)control.Tag;

  if (!char.IsControl(e.KeyChar) && (!char.IsDigit(e.KeyChar) || control.Text.Length >= length))
  {
    e.Handled = true;
  }
}