我找到了带复选框http://www.codeproject.com/Articles/31105/A-ComboBox-with-a-CheckedListBox-as-a-Dropdown的自定义ComboBox,并希望在DataGridView单元格中使用此控件。
据我了解,需要为其创建自定义列类:https://msdn.microsoft.com/en-us/library/7tas5c80.aspx
我完全不明白需要改变什么,尝试写类似,但它不起作用。
public class CheckedComboBoxColumn : DataGridViewColumn
{
public CheckedComboBoxColumn() : base(new CheckedComboBoxCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CalendarCell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(CheckedComboBoxCell)))
{
throw new InvalidCastException("Must be a CheckedComboBoxCell");
}
base.CellTemplate = value;
}
}
public class CheckedComboBoxCell : DataGridViewTextBoxCell
{
public CheckedComboBoxCell() : base()
{
}
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
// Set the value of the editing control to the current cell value.
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
CheckedComboBoxControl ctl =
DataGridView.EditingControl as CheckedComboBoxControl;
}
public override Type EditType
{
get
{
// Return the type of the editing control that CalendarCell uses.
return typeof(CheckedComboBoxControl);
}
}
public override Type ValueType
{
get
{
// Return the type of the value that CalendarCell contains.
return typeof(CheckedComboBox);
}
}
public override object DefaultNewRowValue
{
get
{
// Use the current date and time as the default value.
return new CheckedComboBox();
}
}
}
public class CheckedComboBoxControl : CheckedComboBox, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
public CheckedComboBoxControl()
{
//this.Format = DateTimePickerFormat.Short;
}
// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.
public object EditingControlFormattedValue
{
get
{
//return this.Value.ToShortDateString();
return "";
}
set
{
if (value is String)
{
try
{
// This will throw an exception of the string is
// null, empty, or not in the format of a date.
//this.Value = DateTime.Parse((String)value);
}
catch
{
// In the case of an exception, just use the
// default value so we're not left with a null
// value.
//this.Value = DateTime.Now;
}
}
}
}
// Implements the
// IDataGridViewEditingControl.GetEditingControlFormattedValue method.
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
// Implements the
// IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
//this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
//this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
}
// Implements the IDataGridViewEditingControl.EditingControlRowIndex
// property.
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
// Implements the IDataGridViewEditingControl.EditingControlWantsInputKey
// method.
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
// Let the DateTimePicker handle the keys listed.
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 !dataGridViewWantsInputKey;
}
}
// Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit
// method.
public void PrepareEditingControlForEdit(bool selectAll)
{
// No preparation needs to be done.
}
// Implements the IDataGridViewEditingControl
// .RepositionEditingControlOnValueChange property.
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlDataGridView property.
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingControlValueChanged property.
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}
// Implements the IDataGridViewEditingControl
// .EditingPanelCursor property.
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}
}
}
尝试这样使用:
DataGridViewRow view_row = dataGridView2.Rows[1];
view_row.Cells[0].Value = "Test";
CheckedComboBox cb = new CheckedComboBox();
cb.MaxDropDownItems = 5;
cb.DisplayMember = "Name";
cb.ValueSeparator = ", ";
foreach (var id_to_user_pair in id_to_user)
{
cb.Items.Add(new CCBoxItem(id_to_user_pair.Value, id_to_user_pair.Key), true);
}
cb.UpdateText();
view_row.Cells[1].Value = cb;
你能帮助我吗?