简体   繁体   English

C#Winform DataGrid复选框已选中

[英]C# Winform datagrid checkbox checked

I am adding a checkbox to my datagrid (index 5) - but how do i set the checkbox so "checked" by default? 我在我的数据网格(索引5)中添加了一个复选框-但是如何将复选框设置为默认为“选中”?

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;


        }


    }

I stepped over this, and it seemed to fix my issue 我跨过这个,似乎可以解决我的问题

chk.DefaultCellStyle.NullValue = true; chk.DefaultCellStyle.NullValue = true;

The property Selected refers to whether the cell is focused or not. Selected ”属性是指单元是否聚焦。 try checkBox1.Value = true or try changing it to true in the objects properties window 尝试checkBox1.Value = true或尝试在对象属性窗口中将其更改为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM