Windows 2008Windows VistaDBAWindows 2003.NET 3.0Windows 2000设计 / 图形Windows XP.NET 2.0.NET 3.5C# 2.0初学者C# 3.0开发Windows.NETC#
DataGridViewColourPicker






4.80/5 (12投票s)
用于从 DataGridView 中的下拉列表中选择颜色的自定义 DataGridView 列

引言
这是我多年来开发的一系列 datagridviewcolumn
文章之一。现在我想在CodeProject上分享它们。希望你喜欢。
背景
Datagridview
让你可以在 datagridview
上拥有一个自定义编辑器,这使你可以控制表中数据的编辑方式,以及你希望如何格式化并将数据展示给最终用户。例如,你有一个枚举,你想为一些操作指定数字:运行=1,跳跃=2,玩得开心=3,你将这些数字存储在数据库中而不是操作名称,这样你的最终用户就不需要将这些数字解释为这些操作。他希望看到真正的操作名称。
Using the Code
你应该继承的三个主要类是
DataGridViewColumn
: 这是你在 Visual Studio 中选择的列的类型。你只需在构造函数中指定你希望此列的所有单元格的类型。DataGridViewCell
或DataGridViewTextBoxCell
: 在这个类中,你需要重写Paint
: 用于自定义绘制所有相同类型的单元格,因为它依赖于编辑器。如果显示datagridview
时发生任何错误,此函数是第一个要检查的地方。InitializeEditingControl
: 每次你双击一个单元格或在上面按 F2 时,都会调用此函数,并且datagridview
想要显示一个单元格值的编辑器。因此,在这个函数中,你创建一个编辑器的实例,并为编辑器选择当前值进行更改和显示。请记住,datagridview
中的值以stringvalue
的形式发送,因此如果它是一个数字,它会在initialFormattedValue
中显示单元格的当前值,例如 "43
",你应该将该string
值解析为你的类型。
IDataGridViewEditingControl
接口增加了所有功能,用于使你的编辑器适应datagridview
单元格编辑
//
using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
namespace FarsiLibrary.Win.Controls
{
public class DataGridViewColourPickerCell : DataGridViewTextBoxCell
{
private DateTime selectedDateTime;
private static Type valueType = typeof(byte);
private static Type editType = typeof(DataGridViewColourPickerEditor);
private StringAlignment verticalAlignment;
private StringAlignment horizontalAlignment;
//private FormatInfoTypes format;
public override Type EditType
{
get { return editType; }
}
public override Type ValueType
{
get { return valueType; }
}
public DateTime SelectedDateTime
{
get { return selectedDateTime; }
set { selectedDateTime = value; }
}
/// <summary>
/// Returns the current DataGridView EditingControl
/// as a DataGridViewNumericUpDownEditingControl control
/// </summary>
private DataGridViewColourPickerEditor EditingFADatePicker
{
get { return DataGridView.EditingControl as DataGridViewColourPickerEditor; }
}
private const int RECTCOLOR_LEFT = 0;
private const int RECTCOLOR_TOP = 0;
private const int RECTCOLOR_WIDTH = 20;
private const int RECTTEXT_MARGIN = 0;
private const int RECTTEXT_LEFT =
RECTCOLOR_LEFT + RECTCOLOR_WIDTH + RECTTEXT_MARGIN;
private static StringFormat sf;
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,
DataGridViewPaintParts.All);
if (DataGridView == null)
return;
if (sf == null)
{
sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
}
Color BlockColor = Color.White;
if (value == null || value.GetType() == typeof(DBNull)) return;
BlockColor = Color.FromKnownColor((KnownColor)((byte)value));
Rectangle rec = new Rectangle(cellBounds.X + 3,
cellBounds.Y + 3, cellBounds.Width - 8, cellBounds.Height - 8);
graphics.FillRectangle(new SolidBrush(BlockColor), rec);
graphics.DrawRectangle(Pens.Black, rec);
graphics.DrawString(BlockColor.Name, this.DataGridView.Font,
Brushes.Black, rec, sf);
// if (PartPainted(paintParts, DataGridViewPaintParts.ErrorIcon))
}
private static bool PartPainted(DataGridViewPaintParts paintParts,
DataGridViewPaintParts paintPart)
{
return (paintParts & paintPart) != 0;
}
[DefaultValue("Center")]
public StringAlignment VerticalAlignment
{
get { return verticalAlignment; }
set { verticalAlignment = value; }
}
[DefaultValue("Near")]
public StringAlignment HorizontalAlignment
{
get { return horizontalAlignment; }
set { horizontalAlignment = value; }
}
private static bool IsInState(DataGridViewElementStates currentState,
DataGridViewElementStates checkState)
{
return (currentState & checkState) != 0;
}
/// <summary>
/// Little utility function called by the Paint function to see
/// if a particular part needs to be painted.
///
/// <summary>
/// Determines whether this cell, at the given row index,
/// shows the grid's editing control or not.
/// The row index needs to be provided as a parameter
/// because this cell may be shared among multiple rows.
/// </summary>
private bool OwnsEditor(int rowIndex)
{
if (rowIndex == -1 || DataGridView == null)
return false;
DataGridViewColourPickerEditor editor =
DataGridView.EditingControl as DataGridViewColourPickerEditor;
return editor != null && rowIndex == editor.EditingControlRowIndex;
}
internal void SetValue(int rowIndex, DateTime value)
{
//SelectedDateTime = value;
//if (OwnsEditor(rowIndex))
// EditingFADatePicker.SelectedDateTime = value;
}
public override void InitializeEditingControl(int rowIndex,
object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl
(rowIndex, initialFormattedValue, dataGridViewCellStyle);
DataGridViewColourPickerEditor editor =
DataGridView.EditingControl as DataGridViewColourPickerEditor;
if (editor != null)
{
editor.RightToLeft = DataGridView.RightToLeft;
string formattedValue = initialFormattedValue.ToString();
byte o;
if (byte.TryParse(initialFormattedValue.ToString(), out o))
{
editor.SelectedIndex = o - 1;
//editor.Text = "RED";
}
//if (string.IsNullOrEmpty(formattedValue))
//{
// editor.SelectedDateTime = DateTime.Now;
// editor.mv.MonthViewControl.SetNoneDay();
//}
//else
//{
// editor.SelectedDateTime = DateTime.Parse(formattedValue);
// editor.mv.MonthViewControl.clock.Dt = editor.SelectedDateTime;
//}
}
}
}
public class DataGridViewColourPickerEditor :
ColorPicker, IDataGridViewEditingControl
{
public void ApplyCellStyleToEditingControl
(DataGridViewCellStyle dataGridViewCellStyle)
{
this.SelectedIndexChanged += new EventHandler
(DataGridViewFADateTimePickerEditor_SelectedIndexChanged);
}
void DataGridViewFADateTimePickerEditor_SelectedIndexChanged
(object sender, EventArgs e)
{
EditingControlValueChanged = true;
EditingControlFormattedValue = (byte)8;
if (EditingControlValueChanged == true)
EditingControlDataGridView.NotifyCurrentCellDirty(true);
}
DataGridView _editingControlDataGridView;
public DataGridView EditingControlDataGridView
{
get
{
return _editingControlDataGridView;
}
set
{
_editingControlDataGridView = value;
}
}
object _editingControlFormattedValue;
public object EditingControlFormattedValue
{
get
{
return _editingControlFormattedValue;
}
set
{
_editingControlFormattedValue = value;
}
}
int _editingControlRowIndex;
public int EditingControlRowIndex
{
get
{
return _editingControlRowIndex;
}
set
{
_editingControlRowIndex = value;
}
}
bool _editingControlValueChanged;
public bool EditingControlValueChanged
{
get
{
return _editingControlValueChanged;
}
set
{
_editingControlValueChanged = value;
}
}
public bool EditingControlWantsInputKey
(Keys keyData, bool dataGridViewWantsInputKey)
{
return true;
}
public Cursor EditingPanelCursor
{
get { return Cursors.Default; }
}
public object GetEditingControlFormattedValue
(DataGridViewDataErrorContexts context)
{
if (this.SelectedItem == null)
return 0;
return ((byte)((Color)
((MyColour)this.SelectedItem).Colour).ToKnownColor()).ToString();
}
public void PrepareEditingControlForEdit(bool selectAll)
{
}
public bool RepositionEditingControlOnValueChange
{
get { return true; }
}
}
//[ToolboxBitmap(typeof(DataGridViewFADateTimePickerColumn),
// "DataGridViewFADateTimePickerColumn.bmp")]
public class DataGridViewColourPickerColumn : DataGridViewColumn
{
public DataGridViewColourPickerColumn()
: base(new DataGridViewColourPickerCell())
{
}
//[Browsable(false)]
//[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
//public override DataGridViewCell CellTemplate
//{
// get { return base.CellTemplate; }
// set
// {
// DataGridViewFADateTimePickerCell dataGridViewFADateTimePickerCell =
// value as DataGridViewFADateTimePickerCell;
// if (value != null && dataGridViewFADateTimePickerCell == null)
// throw new InvalidCastException("Value provided for CellTemplate
// must be of type DataGridViewRadioButtonElements.
// DataGridViewRadioButtonCell or derive from it.");
// base.CellTemplate = value;
// }
//}
/// <summary>
/// Small utility function that returns the template cell as a
/// DataGridViewRadioButtonCell.
/// </summary>
//private DataGridViewFADateTimePickerCell FADatePickerCellTemplate
//{
// get { return (DataGridViewFADateTimePickerCell)CellTemplate; }
//}
}
public class ColorPicker : ComboBox
{
private const int RECTCOLOR_LEFT = 4;
private const int RECTCOLOR_TOP = 3;
private const int RECTCOLOR_WIDTH = 40;
private const int RECTTEXT_MARGIN = 3;
private const int RECTTEXT_LEFT = RECTCOLOR_LEFT +
RECTCOLOR_WIDTH + RECTTEXT_MARGIN;
public ColorPicker()
{
this.DrawMode = DrawMode.OwnerDrawFixed;
this.DropDownStyle = ComboBoxStyle.DropDownList;
for (byte i = 1; i < 174; i++)
{
MyColour c = new MyColour();
c.Colour = Color.FromKnownColor((KnownColor)i);
c.Colourid = i;
this.Items.Add(c);
}
this.DisplayMember = "Name";
this.ValueMember = "Colourid";
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
}
base.Dispose(disposing);
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
if (e.State == DrawItemState.Selected || e.State == DrawItemState.None)
e.DrawBackground();
Graphics Grphcs = e.Graphics;
Color BlockColor = Color.Empty;
int left = RECTCOLOR_LEFT;
if (e.Index == -1)
BlockColor = SelectedIndex < 0 ?
BackColor : Color.FromName(SelectedText);
else
BlockColor = ((MyColour)base.Items[e.Index]).Colour;
Grphcs.FillRectangle(new SolidBrush(BlockColor),
left, e.Bounds.Top + RECTCOLOR_TOP, RECTCOLOR_WIDTH,
ItemHeight - 2 * RECTCOLOR_TOP);
Grphcs.DrawRectangle(Pens.Black, left, e.Bounds.Top +
RECTCOLOR_TOP, RECTCOLOR_WIDTH, ItemHeight - 2 * RECTCOLOR_TOP);
Grphcs.DrawString(BlockColor.Name, e.Font, new SolidBrush(ForeColor),
new Rectangle(RECTTEXT_LEFT, e.Bounds.Top,
e.Bounds.Width - RECTTEXT_LEFT, ItemHeight));
base.OnDrawItem(e);
}
protected override void OnDropDownStyleChanged(EventArgs e)
{
if (this.DropDownStyle != ComboBoxStyle.DropDownList)
this.DropDownStyle = ComboBoxStyle.DropDownList;
}
}
public class MyColour
{
byte _colourid;
public byte Colourid
{
get { return _colourid; }
set { _colourid = value; }
}
Color _colour;
public Color Colour
{
get { return _colour; }
set { _colour = value; }
}
}
} //
历史
- 2009年5月17日:最初发布