选中C#Winform datagrid复选框

时间:2017-07-11 09:15:24

标签: c# winforms datagridview

我正在为我的数据网格添加一个复选框(索引5) - 但是如何设置复选框以便#34;选中"默认情况下?

我无法弄清楚如何做到这一点 - 我尝试过不同的版本,但没有任何帮助 - 每次运行代码时,只需选中未选中的复选框。

//编辑 - 更新了整个代码

        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;


        }


    }

3 个答案:

答案 0 :(得分:2)

我跨过了这个,似乎解决了我的问题

chk.DefaultCellStyle.NullValue = true;

答案 1 :(得分:0)

我用你的代码进行了一些实验

chk.DefaultCellStyle.NullValue = true;

并发现它实际上并没有设置细胞的值。 DataGridView使用此属性来格式化单元格的值,例如在绘制时。

这是我的实验代码

    public Form1()
    {
        InitializeComponent();

        Column1.DefaultCellStyle.NullValue = true;
        this.dataGridView1.Rows.Add(10);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow dataGridViewRow in this.dataGridView1.Rows)
        {
            // variable "value" gets null value.
            var value = dataGridViewRow.Cells[Column1.Name].Value;
            if (value != null)
            {
                MessageBox.Show(value.ToString());
            }
        }
    }

所以我的问题是你需要为细胞设置值吗?或者你只是想让你的datagridview绘制检查状态?

您的初始代码无法正常工作的原因可能是您正在调用" popuplateDataGrid()"在表单的构造函数中,这可能不会让datagridview更新单元格的值。也许它与句柄创建有关,但我不确定。我是这样测试的。

        public Form1()
        {
            InitializeComponent();

            // Form1's handle not created yet here
            var value = IsHandleCreated;

            // So, CheckBoxCell's value will not take affect
            popuplateDataGrid();
        }

我建议打电话给" popuplateDataGrid()"在构造函数之外。例如,像这样的加载事件处理程序

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        // I tested your code and it worked.
        popuplateDataGrid();
    }

或者在按钮单击处理程序中。我测试过,它有效。

答案 2 :(得分:-1)

属性Selected指的是细胞是否聚焦。 尝试 checkBox1.Value = true 或尝试在对象属性窗口中将其更改为true