2

J'ai les éléments suivants: DataGridViewCheckBoxCell. le problème est la mise à jour visuelle ne se fait pas immédiatement dans le mode d'édition, seulement quand je le quitteLa mise à jour visuelle DataGridViewCheckBoxCell personnalisée ne fonctionne pas en mode édition

public class CustomDataGridViewCell : DataGridViewCheckBoxCell 
{ 
    protected override void Paint(Graphics graphics, 
           Rectangle clipBounds, Rectangle cellBounds, int rowIndex, 
           DataGridViewElementStates elementState, object value, 
           object formattedValue, string errorText, 
           DataGridViewCellStyle cellStyle, 
           DataGridViewAdvancedBorderStyle advancedBorderStyle, 
           DataGridViewPaintParts paintParts) 
    { 
     base.Paint(graphics, clipBounds, cellBounds, rowIndex, 
      elementState, value, formattedValue, errorText, cellStyle, 
      advancedBorderStyle, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground); 

     var val = (bool?)FormattedValue; 
     var img = val.HasValue && val.Value ? Properties.Resources._checked : Properties.Resources._unchecked; 
     var w = img.Width; 
     var h = img.Height; 
     var x = cellBounds.Left + (cellBounds.Width - w)/2; 
     var y = cellBounds.Top + (cellBounds.Height - h)/2; 
     graphics.DrawImage(img, new Rectangle(x, y, w, h)); 
    } 
} 

Répondre

1

Vous avez besoin de 2 corrections:

  1. Vous devez créer un CustomDataGridViewCheckBoxColumn ainsi que son Le modèle de cellule est défini sur CustomDataGridViewCheckBoxCell.

  2. Au lieu de la propriété FormattedValue, utilisez le paramètre formattedValue.

Voici le code:

public class CustomDataGridViewCheckBoxColumn: DataGridViewCheckBoxColumn 
{ 
    public CustomDataGridViewColumn() 
    { 
     this.CellTemplate = new CustomDataGridViewCheckBoxCell(); 
    } 
} 
public class CustomDataGridViewCheckBoxCell: DataGridViewCheckBoxCell 
{ 
    protected override void Paint(Graphics graphics, 
           Rectangle clipBounds, Rectangle cellBounds, int rowIndex, 
           DataGridViewElementStates elementState, object value, 
           object formattedValue, string errorText, 
           DataGridViewCellStyle cellStyle, 
           DataGridViewAdvancedBorderStyle advancedBorderStyle, 
           DataGridViewPaintParts paintParts) 
    { 
     base.Paint(graphics, clipBounds, cellBounds, rowIndex, 
      elementState, value, formattedValue, errorText, cellStyle, 
      advancedBorderStyle, DataGridViewPaintParts.All & ~DataGridViewPaintParts.ContentForeground); 
     var val = (bool?)formattedValue; 
     var img = val.HasValue && val.Value ? Properties.Resources.Checked : Properties.Resources.UnChecked; 
     var w = img.Width; 
     var h = img.Height; 
     var x = cellBounds.Left + (cellBounds.Width - w)/2; 
     var y = cellBounds.Top + (cellBounds.Height - h)/2; 
     graphics.DrawImage(img, new Rectangle(x, y, w, h)); 
    } 
} 
+0

Eh oui, il était formattedValue le paramètre du problème. Je me sens gêné * soupir * – ihisham