【问题标题】:C# Winform datagrid checkbox checkedC# Winform 数据网格复选框已选中
【发布时间】:2017-07-11 09:15:23
【问题描述】:

我正在向我的数据网格(索引 5)添加一个复选框 - 但我如何将复选框设置为默认“选中”?

我不知道该怎么做 - 我尝试了不同的版本,但没有任何帮助 - 每次我运行代码时,它都会出现未选中的复选框。

// 编辑 - 更新整个代码

        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;


        }


    }

【问题讨论】:

  • 您是否尝试将 ckBox.Value 设置为 true
  • @SamvelPetrosov 您已链接列而不是单元格...
  • 属性Selected指的是单元格是否被聚焦。但是属性Value 指的是单元格的内容。复选框具有boolean 值,因此您需要设置chkBox.Value = true
  • chkBox.Value = true; - 不工作,我试过了。

标签: c# winforms datagridview


【解决方案1】:

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

chk.DefaultCellStyle.NullValue = true;

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多