[英]Datagridview add Checkbox on some cell using <DataGridViewCheckBoxColumn>, not a column
I have a DataGirdView
that is bound to a List. 我有一个绑定到列表的
DataGirdView
。
I would like to add CheckBox
on some cell by DataGridViewCheckBoxCell
, but that is no controllers display on my datagridview. 我想通过
DataGridViewCheckBoxCell
在某些单元格上添加CheckBox
,但这不是控制器显示在我的datagridview上。 Below is my code: 下面是我的代码:
class ColumnNames
{
public string Check { get; set; }
public string Index { get; set; }
public string SubIndex { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public ColumnNames(
string Check,
string Index,
string SubIndex,
string Name,
string Value
)
{
this.Check = Check;
this.Index = Index;
this.SubIndex = SubIndex;
this.Name = Name;
this.Value = Value;
}
}
itemsList.Add(new ColumnNames(Check, Index, SubIndex, Name, DataType));
datagridview.DataSource = itemsList;
datagridview.Rows[0].ReadOnly = true;
datagridview.Rows[0].Cells[0] = new DataGridViewCheckBoxCell();
datagridview.Rows[0].Cells[0].Value = false;
when I run this code, I only get the string 'false' on cell[0]. 当我运行此代码时,仅在cell [0]上得到字符串'false'。 I had monitor this cell using break point, this cell is "Datagirdviewcheckboxcell" , but no checkbox controller on this.
我已经使用断点监视此单元格,此单元格为“ Datagirdviewcheckboxcell”,但此框上没有复选框控制器。 How can I solve this problem?
我怎么解决这个问题? Thanks!
谢谢!
I think you set in this line new ColumnNames(Check, Index, SubIndex, Name, DataType)
Check as "string" with value "false". 我认为您在这一行中设置了
new ColumnNames(Check, Index, SubIndex, Name, DataType)
检查为值为“ false”的“ string”。
Change ColumnNames to: 将ColumnNames更改为:
...
public bool Check { get; set; }
...
public ColumnNames(bool Check, ...)
{
this.Check = Check;
...
}
...
Then just: 然后:
List<ColumnNames> itemsList = new List<ColumnNames>();
itemsList.Add(new ColumnNames(true,"Index", "SubIndex", "Name", "DataType"));
dataGridView1.DataSource = itemsList;
Results in: 结果是:
No need for: datagridview.Rows[0].Cells[0] = new DataGridViewCheckBoxCell();
不需要:
datagridview.Rows[0].Cells[0] = new DataGridViewCheckBoxCell();
In case you need different types of cells in one column you will have to go with custom DataGridViewColumn. 如果在一列中需要不同类型的单元格,则必须使用自定义DataGridViewColumn。
According to this answer: creating a custom column type? 根据此答案: 创建自定义列类型? You have available two tutorials:
您有两个教程:
Then just stick with object type of Check property in your class and in your custom implementation paint cell based on value type. 然后,只需在类和基于值类型的自定义实现绘画单元中坚持Check属性的对象类型即可。
Something like: 就像是:
switch(Check.GetType()){
case typeof(Boolean): /* draw CheckBox */ break;
default: /* draw string */ break; //you can keep string in default with all other types.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.