Cyd Oragorn

Cyd Oragorn

  • NA
  • 3
  • 0

Automatically replace or remove a particular character in DataGridView

Jun 13 2007 9:33 PM

I want the zeroes, '0', in the DataGridView to appear invisible without changing the actual value in the datasource (which is a DataTable). Is there a way to do this automatically (say tinker a property or implement an interface)? I'm trying to avoid creating a loop to check and alter every cell.

Somebody suggested that I should be able to subclass the DataGridViewTextBoxCell and control how it "displays" its contents.

Unfortunately, I'm still at lose on how to implement it. I tried this, but it didn't work:

public class DataGridViewTextBoxCellZeroless : DataGridViewTextBoxCell
{
   public DataGridViewTextBoxCellZeroless() {}
   private object _value;
   public override object Clone()
   {
      return base.Clone();
   }

   public new object Value
   {
      get { return _value; }
      set
      {
         if (value != null && value.ToString() == "0")
            _value = null;
         else
            _value = value;
      }
   }
}

...
DataGridViewCell cell = new DataGridViewTextBoxCellZeroless();

//col1 is a column in my DataGridView
col1.CellTemplate = cell;
...

I figure the Value property is not exactly the one being displayed. Any suggestion would be helpful.