Как изменить изображения datagridviecheckboxcell по умолчанию
Привет , я хочу заменить пользовательские изображения для проверки datagridviewCheckBoxCell и снять флажок.
я пишу ниже коды, но эти коды иногда работают, а иногда при нажатии на ячейку никаких действий не происходит, и большинство перемещает мышь, а затем проверяет или снимает флажок работает!
Что я уже пробовал:
public class MyDataGridViewCheckBoxColumn : DataGridViewColumn { public MyDataGridViewCheckBoxColumn() { CellTemplate = new MyDataGridViewCheckBoxCell(); } } // // // public class MyDataGridViewCheckBoxCell : DataGridViewCheckBoxCell { private static Type cellType = typeof(MyDataGridViewCheckBoxCell); public MyDataGridViewCheckBoxCell() {} protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { // Point _loc = new Point(cellBounds.X + (cellBounds.Width - 16) / 2, cellBounds.Y + (cellBounds.Height - 16) / 2); //bool val = this.Value == null || !Convert.ToBoolean(this.Value) ? false : Convert.ToBoolean(this.Value); bool val = false; if (formattedValue == null) val = false; else val = Convert.ToBoolean(formattedValue); // if (val) g.DrawImage(_checkImage, _loc); else g.DrawImage(_uncheckImage, _loc); } bool _entered; protected override void OnEnter(int rowIndex, bool throughMouseClick) { _entered = true; base.OnEnter(rowIndex, throughMouseClick); } protected override void OnLeave(int rowIndex, bool throughMouseClick) { _entered = false; base.OnLeave(rowIndex, throughMouseClick); } protected override void OnMouseClick(DataGridViewCellMouseEventArgs e) { if (e.RowIndex != -1) { SetImage(); } base.OnMouseClick(e); } private void SetImage() { this.Value = !Convert.ToBoolean(this.Value);//FormattedValue this.DataGridView.InvalidateCell(this); this.DataGridView.NotifyCurrentCellDirty(true); } }