C# 使用<;在某些单元格上添加Datagridview复选框;DataGridViewCheckBoxColumn>;,不是专栏
我有一个绑定到列表的C# 使用<;在某些单元格上添加Datagridview复选框;DataGridViewCheckBoxColumn>;,不是专栏,c#,checkbox,datagridview,C#,Checkbox,Datagridview,我有一个绑定到列表的DataGirdView 我想通过DataGridViewCheckBoxCell在某些单元格上添加复选框,但我的datagridview上没有显示任何控制器。下面是我的代码: class ColumnNames { public string Check { get; set; } public string Index { get; set; } public string SubIndex { get; set; } public str
DataGirdView
我想通过DataGridViewCheckBoxCell
在某些单元格上添加复选框
,但我的datagridview上没有显示任何控制器。下面是我的代码:
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;
运行此代码时,我只在单元格[0]上获得字符串“false”。
我用断点监视这个单元格,这个单元格是“Datagirdviewcheckboxcell”,但没有复选框控制器。
我怎样才能解决这个问题?谢谢 我认为您在这一行中设置了新的列名称(检查、索引、子索引、名称、数据类型)检查为“字符串”,值为“false” 将列名称更改为:
...
public bool Check { get; set; }
...
public ColumnNames(bool Check, ...)
{
this.Check = Check;
...
}
...
那么就:
List<ColumnNames> itemsList = new List<ColumnNames>();
itemsList.Add(new ColumnNames(true,"Index", "SubIndex", "Name", "DataType"));
dataGridView1.DataSource = itemsList;
由于将列属性设置为只读,请尝试删除
datagridview.Rows[0]。readonly=true
lineI可能是错误的,但我相信该复选框只能对布尔属性正确操作。你能控制课堂吗?你能添加一个像public bool CheckBool{get{return bool.Parse(this.Check);}set{this.Check=value.ToString();}}
这样的属性,然后绑定到它吗?但是当我使用这个类时,public bool Check{get;set;}。我将得到一个列复选框。但我只希望有一行或两行有复选框,其他行没有。请查看我的更新。对不起,我没有时间在这里给你写信,这样复杂的解决方案;)
switch(Check.GetType()){
case typeof(Boolean): /* draw CheckBox */ break;
default: /* draw string */ break; //you can keep string in default with all other types.
}