DataGridView扩展:NumericUpDown ,并显示比例
2009-09-22 09:07 Zhang_Chen 阅读(970) 评论(0) 编辑 收藏 举报代码是自己写的,实现功能:NumericUpDown编辑单元格,显示值与最大值的比例
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CuteDataView
{
public class NumericUpDownColumn : DataGridViewColumn
{
public NumericUpDownColumn(): base(new NumericUpDownCell())
{
}
public NumericUpDownColumn(decimal max, decimal min, decimal increment,decimal defaultValue,bool showRect,Color rectColor)
: base(new NumericUpDownCell{Maximum=max,Minimum=min,DefaultValue=defaultValue,Increment=increment,RectColor=rectColor,ShowRect=showRect})
{
}
public decimal Maximum { get; set; }
public decimal Minimum { get; set; }
public decimal Increment { get; set; }
public decimal DefaultValue { get; set; }
public bool ShowRect { get; set; }
public Color RectColor { get; set; }
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 decimal Maximum { get; set; }
public decimal Minimum { get; set; }
public decimal Increment { get; set; }
public decimal DefaultValue { get; set; }
public bool ShowRect { get; set; }
public Color RectColor { get; set; }
public NumericUpDownCell()
{
Maximum = decimal.MaxValue;
Minimum = decimal.MinValue;
Increment = 1;
DefaultValue = 0;
ShowRect = false;
RectColor = Color.FromArgb(50, Color.Magenta);
}
public override Type EditType
{
get { return typeof (NumericUpDownEditingControl); }
}
public override Type ValueType
{
get { return typeof (string); }
}
public override object DefaultNewRowValue
{
get { return DefaultValue; }
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue,
DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
var ctl = DataGridView.EditingControl as NumericUpDownEditingControl;
if (ctl != null)
{
ctl.Maximum = Maximum;
ctl.Minimum = Minimum;
ctl.Increment = Increment;
ctl.Text = Value.ToString();
}
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
if (ShowRect)
{
graphics.FillRectangle(new SolidBrush(RectColor), cellBounds.Left, cellBounds.Top, (cellBounds.Width / Convert.ToSingle(Maximum)) * Convert.ToSingle(Value), cellBounds.Height - 1);
}
}
public override object Clone()
{
return new NumericUpDownCell
{
Maximum = Maximum,
Minimum = Minimum,
ShowRect = ShowRect,
RectColor = RectColor,
Increment = Increment,
DefaultValue = DefaultValue
};
}
}
internal class NumericUpDownEditingControl : NumericUpDown, IDataGridViewEditingControl
{
#region Implementation of IDataGridViewEditingControl
private bool _editingControlValueChanged;
/// <summary>
/// Changes the control's user interface (UI) to be consistent with the specified cell style.
/// </summary>
/// <param name="dataGridViewCellStyle">The <see cref="T:System.Windows.Forms.DataGridViewCellStyle"/> to use as the model for the UI.
/// </param>
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
Font = dataGridViewCellStyle.Font;
ForeColor = dataGridViewCellStyle.ForeColor;
BackColor = dataGridViewCellStyle.BackColor;
}
/// <summary>
/// Determines whether the specified key is a regular input key that the editing control should process or a special key that the <see cref="T:System.Windows.Forms.DataGridView"/> should process.
/// </summary>
/// <returns>
/// true if the specified key is a regular input key that should be handled by the editing control; otherwise, false.
/// </returns>
/// <param name="keyData">A <see cref="T:System.Windows.Forms.Keys"/> that represents the key that was pressed.
/// </param><param name="dataGridViewWantsInputKey">true when the <see cref="T:System.Windows.Forms.DataGridView"/> wants to process the <see cref="T:System.Windows.Forms.Keys"/> in <paramref name="keyData"/>; otherwise, false.
/// </param>
public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
{
switch (keyData & 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;
}
}
/// <summary>
/// Retrieves the formatted value of the cell.
/// </summary>
/// <returns>
/// An <see cref="T:System.Object"/> that represents the formatted version of the cell contents.
/// </returns>
/// <param name="context">A bitwise combination of <see cref="T:System.Windows.Forms.DataGridViewDataErrorContexts"/> values that specifies the context in which the data is needed.
/// </param>
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
/// <summary>
/// Prepares the currently selected cell for editing.
/// </summary>
/// <param name="selectAll">true to select all of the cell's content; otherwise, false.
/// </param>
public void PrepareEditingControlForEdit(bool selectAll)
{
}
/// <summary>
/// Gets or sets the <see cref="T:System.Windows.Forms.DataGridView"/> that contains the cell.
/// </summary>
/// <returns>
/// The <see cref="T:System.Windows.Forms.DataGridView"/> that contains the <see cref="T:System.Windows.Forms.DataGridViewCell"/> that is being edited; null if there is no associated <see cref="T:System.Windows.Forms.DataGridView"/>.
/// </returns>
public DataGridView EditingControlDataGridView { get; set; }
/// <summary>
/// Gets or sets the formatted value of the cell being modified by the editor.
/// </summary>
/// <returns>
/// An <see cref="T:System.Object"/> that represents the formatted value of the cell.
/// </returns>
public object EditingControlFormattedValue
{
get { return Value.ToString(); }
set { Value = decimal.Parse(value.ToString()); }
}
/// <summary>
/// Gets or sets the index of the hosting cell's parent row.
/// </summary>
/// <returns>
/// The index of the row that contains the cell, or –1 if there is no parent row.
/// </returns>
public int EditingControlRowIndex { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the value of the editing control differs from the value of the hosting cell.
/// </summary>
/// <returns>
/// true if the value of the control differs from the cell value; otherwise, false.
/// </returns>
public bool EditingControlValueChanged
{
get { return _editingControlValueChanged; }
set { _editingControlValueChanged = value; }
}
/// <summary>
/// Gets the cursor used when the mouse pointer is over the <see cref="P:System.Windows.Forms.DataGridView.EditingPanel"/> but not over the editing control.
/// </summary>
/// <returns>
/// A <see cref="T:System.Windows.Forms.Cursor"/> that represents the mouse pointer used for the editing panel.
/// </returns>
public Cursor EditingPanelCursor
{
get { return base.Cursor; }
}
/// <summary>
/// Gets or sets a value indicating whether the cell contents need to be repositioned whenever the value changes.
/// </summary>
/// <returns>
/// true if the contents need to be repositioned; otherwise, false.
/// </returns>
public bool RepositionEditingControlOnValueChange
{
get { return false; }
}
#endregion
protected override void OnValueChanged(EventArgs eventargs)
{
_editingControlValueChanged = true;
EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}
}
using System.Drawing;
using System.Windows.Forms;
namespace CuteDataView
{
public class NumericUpDownColumn : DataGridViewColumn
{
public NumericUpDownColumn(): base(new NumericUpDownCell())
{
}
public NumericUpDownColumn(decimal max, decimal min, decimal increment,decimal defaultValue,bool showRect,Color rectColor)
: base(new NumericUpDownCell{Maximum=max,Minimum=min,DefaultValue=defaultValue,Increment=increment,RectColor=rectColor,ShowRect=showRect})
{
}
public decimal Maximum { get; set; }
public decimal Minimum { get; set; }
public decimal Increment { get; set; }
public decimal DefaultValue { get; set; }
public bool ShowRect { get; set; }
public Color RectColor { get; set; }
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 decimal Maximum { get; set; }
public decimal Minimum { get; set; }
public decimal Increment { get; set; }
public decimal DefaultValue { get; set; }
public bool ShowRect { get; set; }
public Color RectColor { get; set; }
public NumericUpDownCell()
{
Maximum = decimal.MaxValue;
Minimum = decimal.MinValue;
Increment = 1;
DefaultValue = 0;
ShowRect = false;
RectColor = Color.FromArgb(50, Color.Magenta);
}
public override Type EditType
{
get { return typeof (NumericUpDownEditingControl); }
}
public override Type ValueType
{
get { return typeof (string); }
}
public override object DefaultNewRowValue
{
get { return DefaultValue; }
}
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue,
DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
var ctl = DataGridView.EditingControl as NumericUpDownEditingControl;
if (ctl != null)
{
ctl.Maximum = Maximum;
ctl.Minimum = Minimum;
ctl.Increment = Increment;
ctl.Text = Value.ToString();
}
}
protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
if (ShowRect)
{
graphics.FillRectangle(new SolidBrush(RectColor), cellBounds.Left, cellBounds.Top, (cellBounds.Width / Convert.ToSingle(Maximum)) * Convert.ToSingle(Value), cellBounds.Height - 1);
}
}
public override object Clone()
{
return new NumericUpDownCell
{
Maximum = Maximum,
Minimum = Minimum,
ShowRect = ShowRect,
RectColor = RectColor,
Increment = Increment,
DefaultValue = DefaultValue
};
}
}
internal class NumericUpDownEditingControl : NumericUpDown, IDataGridViewEditingControl
{
#region Implementation of IDataGridViewEditingControl
private bool _editingControlValueChanged;
/// <summary>
/// Changes the control's user interface (UI) to be consistent with the specified cell style.
/// </summary>
/// <param name="dataGridViewCellStyle">The <see cref="T:System.Windows.Forms.DataGridViewCellStyle"/> to use as the model for the UI.
/// </param>
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
{
Font = dataGridViewCellStyle.Font;
ForeColor = dataGridViewCellStyle.ForeColor;
BackColor = dataGridViewCellStyle.BackColor;
}
/// <summary>
/// Determines whether the specified key is a regular input key that the editing control should process or a special key that the <see cref="T:System.Windows.Forms.DataGridView"/> should process.
/// </summary>
/// <returns>
/// true if the specified key is a regular input key that should be handled by the editing control; otherwise, false.
/// </returns>
/// <param name="keyData">A <see cref="T:System.Windows.Forms.Keys"/> that represents the key that was pressed.
/// </param><param name="dataGridViewWantsInputKey">true when the <see cref="T:System.Windows.Forms.DataGridView"/> wants to process the <see cref="T:System.Windows.Forms.Keys"/> in <paramref name="keyData"/>; otherwise, false.
/// </param>
public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
{
switch (keyData & 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;
}
}
/// <summary>
/// Retrieves the formatted value of the cell.
/// </summary>
/// <returns>
/// An <see cref="T:System.Object"/> that represents the formatted version of the cell contents.
/// </returns>
/// <param name="context">A bitwise combination of <see cref="T:System.Windows.Forms.DataGridViewDataErrorContexts"/> values that specifies the context in which the data is needed.
/// </param>
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
/// <summary>
/// Prepares the currently selected cell for editing.
/// </summary>
/// <param name="selectAll">true to select all of the cell's content; otherwise, false.
/// </param>
public void PrepareEditingControlForEdit(bool selectAll)
{
}
/// <summary>
/// Gets or sets the <see cref="T:System.Windows.Forms.DataGridView"/> that contains the cell.
/// </summary>
/// <returns>
/// The <see cref="T:System.Windows.Forms.DataGridView"/> that contains the <see cref="T:System.Windows.Forms.DataGridViewCell"/> that is being edited; null if there is no associated <see cref="T:System.Windows.Forms.DataGridView"/>.
/// </returns>
public DataGridView EditingControlDataGridView { get; set; }
/// <summary>
/// Gets or sets the formatted value of the cell being modified by the editor.
/// </summary>
/// <returns>
/// An <see cref="T:System.Object"/> that represents the formatted value of the cell.
/// </returns>
public object EditingControlFormattedValue
{
get { return Value.ToString(); }
set { Value = decimal.Parse(value.ToString()); }
}
/// <summary>
/// Gets or sets the index of the hosting cell's parent row.
/// </summary>
/// <returns>
/// The index of the row that contains the cell, or –1 if there is no parent row.
/// </returns>
public int EditingControlRowIndex { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the value of the editing control differs from the value of the hosting cell.
/// </summary>
/// <returns>
/// true if the value of the control differs from the cell value; otherwise, false.
/// </returns>
public bool EditingControlValueChanged
{
get { return _editingControlValueChanged; }
set { _editingControlValueChanged = value; }
}
/// <summary>
/// Gets the cursor used when the mouse pointer is over the <see cref="P:System.Windows.Forms.DataGridView.EditingPanel"/> but not over the editing control.
/// </summary>
/// <returns>
/// A <see cref="T:System.Windows.Forms.Cursor"/> that represents the mouse pointer used for the editing panel.
/// </returns>
public Cursor EditingPanelCursor
{
get { return base.Cursor; }
}
/// <summary>
/// Gets or sets a value indicating whether the cell contents need to be repositioned whenever the value changes.
/// </summary>
/// <returns>
/// true if the contents need to be repositioned; otherwise, false.
/// </returns>
public bool RepositionEditingControlOnValueChange
{
get { return false; }
}
#endregion
protected override void OnValueChanged(EventArgs eventargs)
{
_editingControlValueChanged = true;
EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}
}