16

how to add the checkbox to the datagridview from coding in windows form.

i have a datatable with one column as value=true; and in another datatable i had settings for that column as value='Checkbox'

so if my value is true and checkbox is there the default datatable value cell has to be replaced with checkbox selected true. in that way

if the value is true by default it should be checked in that checkbox..

2
  • methinks stackoverflow has a problem... Commented Jun 17, 2010 at 11:12
  • You've edited the question and now you're asking another thing (which isn't clear, at least for me). If you have another question, ask it separately, because people who might know how to answer that won't read this question (which has the title for the old question) Commented Jun 17, 2010 at 11:20

5 Answers 5

35

If you meant to add a column with checkboxes:

DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "X";
checkColumn.HeaderText = "X";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
checkColumn.FillWeight = 10; //if the datagridview is resized (on form resize) the checkbox won't take up too much; value is relative to the other columns' fill values
dataGridView1.Columns.Add(checkColumn);
Sign up to request clarification or add additional context in comments.

Comments

9

I think the easiest way to add Checkbox column in datagrid view is from the UI

              Step1 : Select the dataGrid at the UI
              Step2: Select Edit Column
              Step3: Click on the column name in edit Columns Window
              Step4:Select column type = "DataGridViewCheckBoxColumn"
              Step5: click ok

Attached a snaphot enter image description here

2 Comments

I think this is the best way!
I don't get why it's the best way. It's the best way only if your datagrid is not linked to some datasource.
4

For these kind of questions you can just add the control through the designer and see what Visual Studio did in the code behind file.

Comments

2

Assuming that you mean how to add a checkbox column to a DataGridView dynamically:

DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
... // set properties as needed here
dataGridView1.Columns.Add(col);

Comments

0

you may also need to set the TrueValue and FalseValue

officeCheckBoxColumn.TrueValue = 1;
officeCheckBoxColumn.FalseValue = 0;

I am wondering if there is way to check all or uncheck all CheckBox by default?

Comments

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.