2009-08-19 6 views
1

J'ai un contrôle personnalisé ComboBox que je veux utiliser dans un DataGridViewCell. J'ai d'abord hérité DataGridViewCell et j'essaie de remplacer la méthode Paint() pour peindre le ComboBox dans la cellule.Comment puis-je dessiner un ComboBox personnalisé dans un DataGridViewCell?

Mon problème est que, après avoir hérité DataGridViewColumn et définir la propriété CellTemplate à une nouvelle instance de ma classe CustomDataGridViewCell, la cellule est gris sans contenu.

La variable de classe cBox est instanciée dans l'objet ctor.

protected override void Paint(Graphics graphics, Rectangle clipBounds, 
    Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, 
    object value, object formattedValue, string errorText, 
    DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle borderStyle, 
    DataGridViewPaintParts paintParts) 
{ 
   // Call MyBase.Paint() without passing object for formattedValue param 
   base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, 
     "", errorText, cellStyle, borderStyle, paintParts); 
    
   // Set ComboBox properties 
   this.cBox.CheckOnClick = true; 
   this.cBox.DrawMode = System.Windows.Forms.DrawMode.Normal; 
   this.cBox.DropDownHeight = 1; 
   this.cBox.IntegralHeight = false; 
   this.cBox.Location = new System.Drawing.Point(cellBounds.X, cellBounds.Y); 
   this.cBox.Size = new System.Drawing.Size(cellBounds.Width, cellBounds.Height); 
   this.cBox.ValueSeparator = ", "; 
   this.cBox.Visible = true; 
   this.cBox.Show(); 
} 

Comment puis-je peindre correctement le ComboBox dans la cellule?

Répondre

1

J'ai fait un changement assez simple qui résout mon problème.

j'ai dû corriger les coordonnées pour être par rapport à la fenêtre au lieu du DataGridView, appelez Controls.Add() la forme la propriété, et repositionner le contrôle devant la DataGridView:

protected override void Paint(Graphics graphics, Rectangle clipBounds, 
    Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, 
    object value, object formattedValue, string errorText, 
    DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle borderStyle, 
    DataGridViewPaintParts paintParts) 
{ 
   // Just paint the border (because it shows outside the ComboBox bounds) 
  this.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, borderStyle); 

    int cellX = this.DataGridView.Location.X + cellBounds.X; 
    int cellY = this.DataGridView.Location.Y + cellBounds.Y; 

   // Create ComboBox and set properties 
   this.cBox = new CheckedComboBox(); 
   this.cBox.DropDownHeight = 1; 
   this.cBox.IntegralHeight = false; 
   this.cBox.Location = new Point(cellX, cellY); 
   this.cBox.Size = new Size(cellBounds.Width, cellBounds.Height); 
   this.cBox.ValueSeparator = ", "; 
    
   // Add to form and position in front of DataGridView 
   this.DataGridView.FindForm.Controls.Add(this.cBox); 
   this.cBox.BringToFront(); 
} 
+1

vous fournir le Hi.Could fichiers à avoir un CheckedCombBox dans DataGridView? – San

Questions connexes