I am using a DataGridView to display locks (by user, assigned by the application) in the associated database.
The user is presented with a list of current locks, showing the relevant information, and the last visible column is a DataGridViewImageColumn
, with the image fed from the application resources.
在末尾还有一个隐藏的DataGridViewCheckBoxColumn
,当用户双击一行的图标时,它被选中,而如果用户双击该行的相同图标,即重置他们的 Select ,则取消选中.
What I'm trying to do is, based on the value of the checkbox, swap the image and tooltip in the CellContentDoubleClick
event, as below, but I'm not seeing the change to the icon or the tooltip.
'ProjectLocked' is the name of the DataGridViewImageColumn
and 'DeleteLock' is the name of the DataGridViewCheckBoxColumn
.
private void dgvProjectLocks_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
// do nothing for the header row
if (e.RowIndex < 0) return;
// caste the object to the correct type
var dgv = (DataGridView)sender;
// Do nothing if the clicked column is not the ProjectLocked column
if (dgv.Columns[e.ColumnIndex].Name != "ProjectLocked") return;
// Get the clicked row
using (var currentRow = dgv.Rows[e.RowIndex])
{
using (var deleteCell = currentRow.Cells["DeleteLock"])
{
switch (Convert.ToBoolean(deleteCell.Value))
{
case true:
currentRow.DefaultCellStyle.BackColor = dgv.DefaultCellStyle.BackColor;
currentRow.DefaultCellStyle.ForeColor = dgv.DefaultCellStyle.ForeColor;
deleteCell.Value = false;
currentRow.Cells["ProjectLocked"].Value = Resources.locked;
deleteCell.ToolTipText = Resources.AddProjectLockDeleteToolTip;
break;
case false:
currentRow.DefaultCellStyle.BackColor = Color.LightGray;
currentRow.DefaultCellStyle.ForeColor = Color.DarkGray;
deleteCell.Value = true;
currentRow.Cells["ProjectLocked"].Value = Resources.unlocked;
deleteCell.ToolTipText = Resources.RemoveProjectLockDeleteToolTip;
break;
}
}
}
}