0

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?

4
  • I think you will have to remove that particular Column (using RemoveAt) and then insert a new one at your required index like grid.Columns.Insert(index, new DataGridViewCheckBoxColumn()) Commented Nov 20, 2012 at 10:19
  • if the datatype of your column is bit or Boolean, the column is displayed as checkBoxColumn automatically, Is it not working like that in your case?? Commented Nov 20, 2012 at 10:19
  • Have you tried creating a DataTable object based on your xml and then binding it to the datagridview instead of creating each column individually? Commented Nov 20, 2012 at 10:24
  • @SamyS.Rathore I didn't know that. I still haven't bound the data to the DataGridView. Thanks for that piece of info :) GxG: No, i was thinking of a more simpler way to go about it. I usually use DataReaders. Not DataTables. Commented Nov 20, 2012 at 10:37

3 Answers 3

3

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

1 Comment

Tried this method and it works fine. I guess I'll go ahead with this one. Thanks a lot :)
1

You will have to remove the current column from your collection and push in a new column of the desired type in that position.

you can use grid.RemoveAt(yourindex); to remove

then you can insert the desired one like

grid.Columns.Insert(yourindex, new DataGridViewCheckBoxColumn())

Or you may just use grid.Columns.Add(new DataGridViewCheckBoxColumn()) and then set the DisplayIndex property of this column to your required index.

1 Comment

I tried this method and it does work. However it will make me repeat code many times because I have some more columns which needs to have checkbox columns apart from the one I showed here in my question. So I think its better to go with Flynn1179 method so that I can do it straight up only once. Thank you for your help nevertheless :)
0

have you tried:

dgvDocDisplay.Columns[2] = new DataGridViewCheckBoxColumn();

?

this

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

indeed adds a new column thus the Columns.Add()

2 Comments

It is a get property, this won't even compile ! ! you can use comments to seek clarifications
Then maybe use the new declaration instead of declaring the count of the columns.

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.