【发布时间】: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