0

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!

2
  • because you set your column property readonly, try removing datagridview.Rows[0].ReadOnly = true; line Commented Jun 12, 2017 at 5:59
  • I could be wrong, but I believe the checkbox will only operate properly against a boolean property. Do you have control over the class? Can you add a property like public bool CheckBool { get { return bool.Parse(this.Check); } set { this.Check = value.ToString(); } } and then bind to that instead? Commented Jun 12, 2017 at 6:17

1 Answer 1

1

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.
}
Sign up to request clarification or add additional context in comments.

2 Comments

But when I use this class public bool Check { get; set; }. I will got a column checkbox. But I only like to have one or 2 rows have checkbox, the others not.
Check out my update. Sorry do not have time to write you here so complex solution like that ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.