Search code examples
c#datagridviewdatagridviewcheckboxcell

Adding a DataGridViewCheckBoxColumn


I have added a bunch of columns to a DataGridView like this through code.

dgvDocDisplay.ColumnCount = 22;
dgvDocDisplay.Columns[0].Name = "Tag";
dgvDocDisplay.Columns[1].Name = "[ ]";
dgvDocDisplay.Columns[2].Name = "#";
dgvDocDisplay.Columns[3].Name = "Type";
dgvDocDisplay.Columns[4].Name = "Reference";

Now I want to make the 3rd column display checkboxes (the rows of that column, not the header). I did quite a bi of search and came across articles like this one but the method shown there would only insert a new column with checkboxes.

DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
dgvDocDisplay.Columns.Add(col);

What I need is to add a checkbox column to an already existing column. Unfortunately I couldn't find anything regarding that.

How can I add checkboxes to an existing column?


Solution

  • If you're adding the columns yourself, why not add the type of column you need when you do that? Like this:

    dgvDocDisplay.Columns.AddRange(
        new DataGridViewColumn[]
        {
            new DataGridViewTextBoxColumn { Name = "Tag" },
            new DataGridViewTextBoxColumn { Name = "[ ]" },
            new DataGridViewCheckBoxColumn { Name = "#" },
            new DataGridViewTextBoxColumn { Name = "Type" }
            // etc
        });