Search code examples
c#datagridviewdatagridviewcolumn

Datagridview Format Column Issue


I am trying to format 2 columns in two different formats

first column 4 digit width, i need the output to be 123.4 when the user types in 1234.

i tried using

__dgw.Columns["column"].DefaultCellStyle.Format = "N1";

but the output is 1,234.0 i dont want commas and just need 123.4 i tried d1 etc

is there such a thing as a masked column?

i also need a way a way to create another column with a mask of ##-##-##?

thank you in advance for your help


Solution

  • If you intend on using this type of behavior more frequently in the future, consider the following links for reference - particularly the last:

    If this is a one shot deal, perhaps less work would be the following:

    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;
        }
      }
    }
    

    This should provide the exact results desired while leaving the underlying value in tact.

    ╔════════════╦═══════════╦═══════════════════╗
    ║ User Input ║ Displayed ║ Actual Cell Value ║
    ╠════════════╬═══════════╬═══════════════════╣
    ║    1234    ║   123.4   ║       1234        ║
    ║   123456   ║  12-34-56 ║      123456       ║
    ╚════════════╩═══════════╩═══════════════════╝
    

    This also assumes you've limited the user to numeric entries of lengths 4 and 6 respectively, which can be done as follows:

    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;
      }
    }