c# - 一个数据网格视图列中的两个复选框
问题描述
如何在数据网格视图列中添加两个复选框?
解决方案
好吧,这不是简单的任务。但可以做到:) 简单的用户控制不是解决方案。
您必须为单元格创建适当的模板,然后创建具有适当界面的控件(可能是用户控件)。
首先,您必须创建新的控件类型。我认为它可以是简单的用户控件和一些额外的东西:
class CheckBoxesState
{
public bool Ch1Checked {get;set;}
public bool Ch2Checked {get;set;}
}
class CheckBoxesControl: UserControl, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
CheckBoxesState state;
// Implements the IDataGridViewEditingControl.EditingControlFormattedValue
// property.
public object EditingControlFormattedValue
{
get { return state; }
set
{
if(value is CheckBoxesState)
{
state = value;
//change checkboxes state in you user control
}
}
}
// 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;
}
// 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)
{
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;
}
}
protected override void OnValueChanged(EventArgs eventargs)
{
// Notify the DataGridView that the contents of the cell
// have changed.
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}
接下来,您必须创建新的单元格类型。
public class CheckBoxesCell : DataGridViewCell
{
public CheckBoxesCell()
: base()
{
}
public override Type EditType
{
get
{
// Return the type of the editing control that cell uses.
return typeof(CheckBoxesControl);
}
}
public override Type ValueType
{
get
{
// Return the type of the value that Cell contains.
return typeof(CheckBoxesState);
}
}
public override object DefaultNewRowValue
{
get
{
// Use the current date and time as the default value.
return new CheckBoxesState();
}
}
}
您应该创建的下一件事是数据网格的新列类型:
public class CheckBoxesColumn : DataGridViewColumn
{
public CheckBoxesColumn() : base(new CheckBoxesCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
// Ensure that the cell used for the template is a CheckBoxesCell.
if (value != null &&
!value.GetType().IsAssignableFrom(typeof(CheckBoxesCell)))
{
throw new InvalidCastException("Must be a CheckBoxesCell");
}
base.CellTemplate = value;
}
}
}
这应该就是全部了。现在您只需创建 CheckBoxesColumn 并将其添加到您的数据网格中。
推荐阅读
- elasticsearch - 需要对文档内部数组对象进行聚合 - ElasticSearch
- react-native - ReactNative - Tcomb-form-native:以编程方式启用/禁用用户输入
- ejabberd - ejabberd如何在windows上编译
- swift - 第一次从firebase检索图像后在本地保存图像(swift 4.2)
- logging - Stackdriver 上的 Apache 访问日志
- sql - 如何选择 n 列和一个 SUM,同时仅按一些非聚合列分组?
- c++ - regexec 和 pcre_exec 有什么区别?
- linux - 特定文件夹中的 Git 克隆存储库,但保留默认文件夹名称
- r - 使用 R 重新投影 MODIS 数据(结果为 NA 或没有空间范围)
- javascript - 为什么我在 webassembly 中导出的快速排序比纯 javascript 实现慢?