Try this version deriving from "DataGridColumnStyle" instead of
DataGridTextBoxColumn. You will need to re-integrate the functionality
provided by it's previous base-type of
DataGridReadOnlyByCellTextBoxColumn but I think it should work for you.
Regards,
GM
http://nonspect.com
//BEGIN CODE SNIP
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
namespace My.Windows.Forms
{
public class DataGridNumericUpDownColumn : DataGridColumnStyle
{
private NumericUpDown _numericUpDown = new NumericUpDown();
// The isEditing field tracks whether or not the user is
// editing data with the hosted control.
private bool isEditing;
public DataGridNumericUpDownColumn() : base()
{
_numericUpDown.Visible = false;
_numericUpDown.Minimum = 0;
_numericUpDown.Maximum = 1000;
//_numericUpDown.Leave += new
System.EventHandler(this.numericUpDown_Leave);
}
protected override void Abort(int rowNum)
{
isEditing = false;
_numericUpDown.ValueChanged -= new
EventHandler(numericUpDown_ValueChanged);
Invalidate();
}
protected override bool Commit(CurrencyManager dataSource, int
rowNum)
{
_numericUpDown.Bounds = Rectangle.Empty;
_numericUpDown.ValueChanged -= new
EventHandler(numericUpDown_ValueChanged);
if (!isEditing)
return true;
isEditing = false;
try
{
SetColumnValueAtRow(dataSource, rowNum, _numericUpDown.Value);
}
catch (Exception)
{
Abort(rowNum);
return false;
}
Invalidate();
return true;
}
protected override void Edit(CurrencyManager source, int rowNum,
Rectangle bounds, bool readOnly, string instantText, bool
cellIsVisible)
{
decimal value;
if(((System.Data.DataRowView)source.Current).IsNew)
value = 0;
else
value = (decimal) GetColumnValueAtRow(source, rowNum);
if(cellIsVisible && !ReadOnly
//&& !GetReadOnlyValueFromFormulas(rowNum,
this.DataGridTableStyle.DataGrid.CurrentCell.ColumnNumber, source)
)
{
_numericUpDown.Bounds = new Rectangle(
bounds.X + 2,
bounds.Y + 2,
bounds.Width - 4,
bounds.Height - 4);
_numericUpDown.Value = value;
_numericUpDown.Visible = true;
_numericUpDown.ValueChanged += new
EventHandler(numericUpDown_ValueChanged);
//this.DataGridTableStyle.DataGrid.Scroll += new
EventHandler(DataGrid_Scroll);
}
else
{
_numericUpDown.Value = value;
_numericUpDown.Visible = false;
}
if(_numericUpDown.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}
protected override Size GetPreferredSize(Graphics g, object value)
{
return new Size(100, _numericUpDown.PreferredHeight + 4);
}
protected override int GetMinimumHeight()
{
return _numericUpDown.PreferredHeight + 4;
}
protected override int GetPreferredHeight(Graphics g, object value)
{
return _numericUpDown.PreferredHeight + 4;
}
protected override void Paint(Graphics g, Rectangle bounds,
CurrencyManager source, int rowNum)
{
Paint(g, bounds, source, rowNum, false);
}
protected override void Paint(Graphics g, Rectangle bounds,
CurrencyManager source, int rowNum, bool alignToRight)
{
Paint(g, bounds, source, rowNum, Brushes.Red, Brushes.Blue,
alignToRight);
}
protected override void Paint(Graphics g, Rectangle
bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush
foreBrush, bool alignToRight)
{
object obj = GetColumnValueAtRow(source, rowNum);
//use NullText or _numericUpDown.Minimum.ToString() here...
string sValue = obj == null ?
this.NullText : obj.ToString();
Rectangle rect = bounds;
g.FillRectangle(backBrush, rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(sValue, this.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
}
#region Public Properties
#region Data
[Category("Data")]
public int DecimalPlaces
{
get
{
return _numericUpDown.DecimalPlaces;
}
set
{
_numericUpDown.DecimalPlaces = value;
}
}
[Category("Data")]
public decimal Increment
{
get
{
return _numericUpDown.Increment;
}
set
{
_numericUpDown.Increment = value;
}
}
[Category("Data")]
public decimal Maximum
{
get
{
return _numericUpDown.Maximum;
}
set
{
_numericUpDown.Maximum = value;
}
}
[Category("Data")]
public decimal Minimum
{
get
{
return _numericUpDown.Minimum;
}
set
{
_numericUpDown.Minimum = value;
}
}
public object Tag
{
get
{
return _numericUpDown.Tag;
}
set
{
_numericUpDown.Tag = value;
}
}
[Category("Data")]
public bool ThousandsSeparator
{
get
{
return _numericUpDown.ThousandsSeparator;
}
set
{
_numericUpDown.ThousandsSeparator = value;
}
}
#endregion
#region Appearance
[Category("Appearance")]
public bool Hexadecimal
{
get
{
return _numericUpDown.Hexadecimal;
}
set
{
_numericUpDown.Hexadecimal = value;
}
}
[Category("Appearance")]
public RightToLeft RightToLeft
{
get
{
return _numericUpDown.RightToLeft;
}
set
{
_numericUpDown.RightToLeft = value;
}
}
[Category("Appearance")]
public HorizontalAlignment TextAlign
{
get
{
return _numericUpDown.TextAlign;
}
set
{
_numericUpDown.TextAlign = value;
}
}
[Category("Appearance")]
public LeftRightAlignment UpDownAlign
{
get
{
return _numericUpDown.UpDownAlign;
}
set
{
_numericUpDown.UpDownAlign = value;
}
}
[Category("Appearance")]
public decimal Value
{
get
{
return _numericUpDown.Value;
}
set
{
_numericUpDown.Value = value;
}
}
#endregion
#region Behavior
[Category("Behavior")]
public bool InterceptArrowKeys
{
get
{
return _numericUpDown.InterceptArrowKeys;
}
set
{
_numericUpDown.InterceptArrowKeys = value;
}
}
#endregion
#endregion
protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
if (_numericUpDown.Parent != null)
{
_numericUpDown.Parent.Controls.Remove(_numericUpDown);
}
if (value != null)
{
value.Controls.Add(_numericUpDown);
}
}
private void numericUpDown_ValueChanged(object sender, EventArgs e)
{
this.isEditing = true;
base.ColumnStartedEditing(_numericUpDown);
}
}
}
//END CODE SNIP