[英]C# Winform datagrid checkbox checked
我在我的數據網格(索引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;
}
}
我跨過這個,似乎可以解決我的問題
chk.DefaultCellStyle.NullValue = true;
“ Selected
”屬性是指單元是否聚焦。 嘗試checkBox1.Value = true
或嘗試在對象屬性窗口中將其更改為true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.