0

I am adding a checkbox to my datagrid (index 5) - but how do i set the checkbox so "checked" by default?

I can't figure out how to do it - i've tried different versions, but nothing helps - everytime i run the code it just comes up with the checkbox unchecked.

// Edit - updated the entire code

        public void popuplateDataGrid()
    {

        selectQueryString = "SELECT LinesEntry.item, LinesEntry.Description, LinesEntry.deliver * -1 as 'Bestilt', i.QuantityPrColi as 'Kolli antal'  FROM LinesEntry inner join Orders on Orders.OrderNo = linesEntry.OrderNo inner join inventory i on i.item = linesEntry.item where Orders.Orderno='23838' ";

        sqlDataAdapter = new SqlDataAdapter(selectQueryString, KompasInterface.SqlConnectionStringCompany);
        sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);

        dataTable = new DataTable();
        sqlDataAdapter.Fill(dataTable);
        bindingSource = new BindingSource();
        bindingSource.DataSource = dataTable;

        // Add them to the list 
        dataGridItems.DataSource = bindingSource;

        //Item Data Source
        string selectQueryStringItem = "SELECT Supplier, Supplier + ' - ' + Name as Name From Suppliers";

        SqlDataAdapter sqlDataAdapterItem = new SqlDataAdapter(selectQueryStringItem, KompasInterface.SqlConnectionStringCompany);
        SqlCommandBuilder sqlCommandBuilderItem = new SqlCommandBuilder(sqlDataAdapterItem);

        DataTable dtSupplier = new DataTable();
        sqlDataAdapterItem.Fill(dtSupplier);
        BindingSource bindSourceSupplier = new BindingSource();
        bindSourceSupplier.DataSource = dtSupplier;

        //Adding  Month Combo
        DataGridViewComboBoxColumn ColumnMonth = new DataGridViewComboBoxColumn();
        ColumnMonth.DataPropertyName = "Supplier";
        ColumnMonth.HeaderText = "Leverandør nr.";
        ColumnMonth.Width = 200;

        ColumnMonth.DataSource = bindSourceSupplier;
        ColumnMonth.ValueMember = "Supplier";
        ColumnMonth.DisplayMember = "Name";

        ColumnMonth.AutoComplete = true;


        dataGridItems.Columns.Add(ColumnMonth);

        DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn(false);
        dataGridItems.Columns.Add(chk);
        chk.HeaderText = "Medtag";
        chk.Name = "Include";
        chk.FalseValue = false;
        chk.TrueValue = true;
        chk.Selected = true;
        //chk.Value = true;


        foreach (DataGridViewRow row in dataGridItems.Rows)
        {

            DataGridViewCheckBoxCell chkBox = (DataGridViewCheckBoxCell)row.Cells[5];
            chkBox.Value = true;
            if (chkBox.Value == chkBox.TrueValue)
            {
                chkBox.Value = chkBox.FalseValue;
            }
            else
            {
                chkBox.Value = chkBox.TrueValue;
            }

            chkBox.Value = true;


        }


    }
5
  • 2
    Did you try to set the ckBox.Value to true? Commented Jul 11, 2017 at 9:18
  • @SamvelPetrosov You've linked column instead of cell... Commented Jul 11, 2017 at 9:21
  • Possible duplicate of Check/Uncheck a checkbox on datagridview Commented Jul 11, 2017 at 9:24
  • The property Selected refers to whether the cell is focused or not. The property Value however refers to the contents of the cell. A checkbox has a boolean value, therefore you need to set chkBox.Value = true Commented Jul 11, 2017 at 9:26
  • chkBox.Value = true; - is not working, i have tried that. Commented Jul 11, 2017 at 10:57

2 Answers 2

2

I stepped over this, and it seemed to fix my issue

chk.DefaultCellStyle.NullValue = true;

Sign up to request clarification or add additional context in comments.

Comments

-1

The property Selected refers to whether the cell is focused or not. try checkBox1.Value = true or try changing it to true in the objects properties window

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.