[英]How to create custom property of DataGridViewTextBoxColumn in c# win. form
How can i create a property named IsCheck (value true/false) in DataGridViewTextBoxColumn ? 如何在DataGridViewTextBoxColumn中创建名为IsCheck (值为true / false)的属性?
i searched the internet but cannot find a solution to create property. 我在互联网上搜索,但找不到创建财产的解决方案。 Please help me with a code snippet or so.
请提供代码片段来帮助我。
In addition to TaW's response, I'd suggest you to override Clone()
function in order to avoid troubles with the Designer
. 除了TaW的响应之外,建议您重写
Clone()
函数,以避免Designer
遇到麻烦。
class myCheckedColumn : DataGridViewTextBoxColumn
{
public bool IsChecked { get; set; }
public myCheckedColumn () { IsChecked = false; }
public myCheckedColumn (bool checked_) { IsChecked = checked_; }
public override object Clone()
{
myCheckedColumn clone = (myCheckedColumn)base.Clone();
myCheckedColumn.IsChecked = IsChecked;
return clone;
}
}
Well, you can create it all in the usual way: 好了,您可以按照通常的方式创建所有内容:
class myCheckedColumn : DataGridViewTextBoxColumn
{
public bool IsChecked { get; set; }
public myCheckedColumn () { IsChecked = false; }
public myCheckedColumn (bool checked_) { IsChecked = checked_; }
}
Now add it to the DataGridView
DGV's Columns
collection: 现在将其添加到
DataGridView
DGV的Columns
集合中:
myCheckedColumn checkColumn = new myCheckedColumn (true);
checkColumn.Name = "checkColumn";
checkColumn.HeaderText = "some Title";
checkColumn.MinimumWidth = 120;
DGV.Columns.Insert(someindex, checkColumn);
And finally we can test it: 最后我们可以对其进行测试:
private void DGV_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
int col = e.ColumnIndex;
myCheckedColumn column = DGV.Columns[col] as myCheckedColumn ;
if (column != null)
{
Console.WriteLine(column.IsChecked.ToString());
column.IsChecked = !column.IsChecked;
}
}
Note: This is a Column
, as you have requested, not a Cell
! 注意:这是您所要求的
Column
,而不是Cell
! So it has one value per inserted instance of that column.. To create a custom DataGridViewTextBoxCell
you would do pretty much the same.. 因此,该列的每个插入实例具有一个值。要创建自定义
DataGridViewTextBoxCell
您将执行几乎相同的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.