>>>Tried to post this before and it seemed to hang. Pardon if this is a double post>>>
I have a DataGridView with three custom column controls. One is for a date picker, the code for which I
found online. I modified that to give me a time-only date picker. I found that someone had taken the same code and modified it to produce a NumericUpDown spin control for DataGridViews. I'm using this custom NumericUpDown control and it works great except for one feature - when the initial value of the data it displays is zero, it will not display the zero. It displays all other initial values, but leaves the cell blank for a zero.
My question is how to get the initial value of zero to display?
Below is the code. Just add it to your project, change the namespace if you wish, and then pick NumericUpDownColumn for ColumnType in any DataGridView column.
I happen to be using an ArrayList of objects as my DataSource to the grid (via a BindingSource), but any data source should exhibit the behavior.
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
namespace EpiModule
{
public class NumericUpDownColumn : DataGridViewColumn
{
public NumericUpDownColumn() : base(new NumericUpDownCell())
{
}
public NumericUpDownColumn(DataGridViewCell cell)
: base(cell)
{
}
public override DataGridViewCell CellTemplate
{
get { return base.CellTemplate; }
set {
if ((value != null) && !value.GetType().IsAssignableFrom(typeof(NumericUpDownCell)))
{
throw new InvalidCastException("Must be a NumericUpDownCell");
}
base.CellTemplate = value;
}
}
}
public class NumericUpDownCell : DataGridViewTextBoxCell
{
public NumericUpDownCell()
{
this.Style.Format = "#";
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
NumericUpDownEditingControl ctl = (NumericUpDownEditingControl)DataGridView.EditingControl;
ctl.Value = Convert.ToDecimal(this.Value);
}
public override Type EditType
{
get { return typeof(NumericUpDownEditingControl); }
}
public override Type ValueType
{
get { return typeof(int); }
}
public override object DefaultNewRowValue
{
get { return 0; }
}
}
class NumericUpDownEditingControl : NumericUpDown, IDataGridViewEditingControl
{
private DataGridView dataGridViewControl;
private bool valueIsChanged = false;
private int rowIndexNum;
protected bool initializing = false;
public NumericUpDownEditingControl()
{
initializing = true;
this.Minimum = (decimal)0;
this.DecimalPlaces = 0;
this.Maximum = 120;
initializing = false;
}
virtual public object EditingControlFormattedValue
{
get { return this.Value.ToString("#"); }
set
{
if (value is int)
{
this.Value = int.Parse(Value.ToString());
}
else if (value is decimal)
{
this.Value = decimal.Parse(value.ToString());
}
}
}
virtual public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return this.Value.ToString("#");
}
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
this.ForeColor = dataGridViewCellStyle.ForeColor;
this.BackColor = dataGridViewCellStyle.BackColor;
}
public int EditingControlRowIndex
{
get { return rowIndexNum; }
set { rowIndexNum = value; }
}
public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
{
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
case Keys.Home:
case Keys.End:
case Keys.PageDown:
case Keys.PageUp:
return true;
default:
return false;
}
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public bool RepositionEditingControlOnValueChange
{
get { return false; }
}
public DataGridView EditingControlDataGridView
{
get { return dataGridViewControl; }
set { dataGridViewControl = value; }
}
public bool EditingControlValueChanged
{
get { return valueIsChanged; }
set { valueIsChanged = value; }
}
public System.Windows.Forms.Cursor EditingControlCursor
{
get { return base.Cursor; }
}
System.Windows.Forms.Cursor IDataGridViewEditingControl.EditingPanelCursor
{
get { return EditingControlCursor; }
}
protected override void OnValueChanged(EventArgs eventargs)
{
if (!initializing)
{
valueIsChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true); base.OnValueChanged(eventargs);
}
}
}