Why my NumericUpDown DataGrid Column can't set the value for new r

G

Guest

I've make some NumericUpDownColumn class that inherited the
DataGridTextBoxColumn and override the SetColumnValueAtRow method as below:
protected override void
SetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, int rowNum,
object value)
{
try
{
base.SetColumnValueAtRow(source, _currentEditRowNum, numericUpDown.Value);
Commit(source, _currentEditRowNum);
}
catch (Exception)
{
Abort(rowNum);
}
}
And also override other methods such as Edit etc., In the
numericUpDown_Leave method, I call the SetColumnValueAtRow method.
I found it works fine for the rows that already exists in the datasource
which bound to DataGrid, but for the new added row, after I set the value and
click the mouse to other row, the value of the new row dispears. Seems that
it can't add row & set value for new row.

Does anyone could help me for this ?
Thanks!
 
G

GM3TEN

Hi,

You may find the ArgumentException that you are "catching" to be very
informative in this case. Then again, you might not.

Try replacing your "catch" block with the following...

catch(Exception ex)
{
System.Diagnostics.Debug.Assert(
false, ex.GetBaseException().Message.ToString());
this.Abort(rowNum);
}

....and if you still need help you may want to consider posting the
"NumericUpDownColumn" class code because "as is" there is not enough
information in the OP to help you.

Regards,
GM
http://nonspect.com
 
G

Guest

No, I can't get any error, and even I've made the breakpoint in the Abort
method, no break happened.

Here is the complete code for this class, wish you could help me :
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

namespace My.Windows.Forms
{
public class DataGridNumericUpDownColumn :
DataGridReadOnlyByCellTextBoxColumn
{
#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

#region Private Properties
private NumericUpDown _numericUpDown = new NumericUpDown();
private CurrencyManager _source;
private int _currentEditRowNum = 0;
#endregion

#region Constructror
public DataGridNumericUpDownColumn() : base()
{

_numericUpDown.Visible = false;
_numericUpDown.Leave += new System.EventHandler(this.numericUpDown_Leave);
//_numericUpDown.LostFocus += new
System.EventHandler(this.numericUpDown_Leave);
_numericUpDown.MouseLeave += new
System.EventHandler(this.numericUpDown_Leave);
}
#endregion

#region Protected & Private Methods
protected override void Edit(System.Windows.Forms.CurrencyManager source,
int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string
instantText, bool cellIsVisible)
{
if (!ReadOnly &&
!GetReadOnlyValueFromFormulas(rowNum,
this.DataGridTableStyle.DataGrid.CurrentCell.ColumnNumber, source) &&
cellIsVisible
)
{
try
{
_numericUpDown.Value = decimal.Parse(GetColumnValueAtRow(source,
rowNum).ToString());
//System.Diagnostics.Debug.WriteLine(_numericUpDown.Value.ToString());
}
catch(System.Exception ex)
{
_numericUpDown.Value = _numericUpDown.Minimum;
//System.Diagnostics.Debug.WriteLine(ex.ToString());
}
if (_numericUpDown.Parent != this.TextBox.Parent)
{
_numericUpDown.Parent = this.TextBox.Parent;
}
Rectangle rect = this.DataGridTableStyle.DataGrid.GetCurrentCellBounds();
_numericUpDown.Location = rect.Location;
_numericUpDown.Size = new Size(rect.Width, rect.Height);


_numericUpDown.Show();
_numericUpDown.BringToFront();
_source = source;
_currentEditRowNum = rowNum;
this.DataGridTableStyle.DataGrid.Scroll += new
EventHandler(DataGrid_Scroll);

}
else
{
EndEdit();
}

if (!_numericUpDown.Visible)
{
DataGridTableStyle.DataGrid.Invalidate(bounds);
}
}


protected override void Abort(int rowNum)
{
Invalidate();
}
protected override void
SetColumnValueAtRow(System.Windows.Forms.CurrencyManager source, int
rowNum, object value)
{

try
{
base.SetColumnValueAtRow(source, _currentEditRowNum,
_numericUpDown.Value);
Commit(source, _currentEditRowNum);
}
catch (Exception ex)
{
Abort(_currentEditRowNum);
}
}
private void DataGrid_Scroll(object sender, EventArgs e)
{
this.DataGridTableStyle.DataGrid.Scroll -= new
EventHandler(DataGrid_Scroll);
_numericUpDown.Hide();
EndEdit();
}



private void numericUpDown_Leave(object sender, System.EventArgs e)
{
SetColumnValueAtRow(_source, _currentEditRowNum, _numericUpDown.Value);
this.DataGridTableStyle.DataGrid.Scroll -= new
EventHandler(DataGrid_Scroll);
_numericUpDown.Hide();
base.HideEditBox();
}
#endregion
}
}
 
G

GM3TEN

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
 
G

GM3TEN

I posted some code in a different message below but I just want to
mention that you might have success with your original class if you add
a "ValueChanged" handler and tell the DataGridColumnStyle to alert the
DataGrid that the user started editing the column.

private void numericUpDown_ValueChanged(object sender, EventArgs e)
{
base.ColumnStartedEditing(_numericUpDown);
}

Anyway, just a thought. I didn't try it myself.

Regards,
GM
http://nonspect.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top