0
有些奇怪。我有一個DataGridView與一個未綁定的CheckBox列。當ComboBox的選定項目更改時,DataGridView會更新DataSource(根據ComboBox中選擇的內容,這裏沒有綁定,只是一個條件語句)。 DataSource更新後,我遍歷一個集合並相應地設置CheckBox列的值。這工作正常。但是,當我決定嘗試在構造函數中設置ComboBox的SelectedItem,以便在Form加載時在ComboBox中有一個值時,同樣的方法無法更新CheckBox單元格的狀態,即使逐步進入調試器顯示值已被設置爲true。無法從表單構造函數中設置DataGridViewCheckBoxCell.Value
public LocksForm()
{
InitializeComponent();
AddDataGridColumns();
GetLookupData();
cboLockType.SelectedIndexChanged += cboLockType_SelectedIndexChanged;
//Setting this fires the event but the checkboxes in the grid remain unchecked
cboLockType.SelectedItem = cboLockType.Items[0];
}
private void cboLockType_SelectedIndexChanged(object sender, EventArgs e)
{
//Bind the data to the grid etc.
UpdateLocks()
}
// This works but not when fired from the Form constructor, only when user selects a value from ComboBox at Runtime
private void UpdateLocks()
{
if (_lockedIDs != null && _lockedIDs.Any())
{
for (int j = 0; j < dgLocks.RowCount; j++)
{
if (_lockedIDs.Contains((Guid)dgLocks[0, j].Value))
{
dgLocks[1, j].Value = true;
}
}
}
}