Reputation: 5
I have a DataGirdView
that is bound to a List.
I would like to add CheckBox
on some cell by DataGridViewCheckBoxCell
, but that is no controllers display on my 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]. I had monitor this cell using break point, this cell is "Datagirdviewcheckboxcell" , but no checkbox controller on this. How can I solve this problem? Thanks!
Upvotes: 0
Views: 206
Reputation: 236
I think you set in this line new ColumnNames(Check, Index, SubIndex, Name, DataType)
Check as "string" with value "false".
Change ColumnNames to:
...
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();
In case you need different types of cells in one column you will have to go with custom 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.
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.
}
Upvotes: 1