2016-06-09 9 views
1

So habe ich begonnen, meine eigene benutzerdefinierte Datagridview-Spalte und Zellklasse abgeleitet von Kombinationsfeld zu machen:Wie EditingControlShowing von abgeleiteten DataGridViewColourComboBoxCell Klasse aufzurufen

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Globalization; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using Teigha.TD; 

namespace TruckleSoft 
{ 
    class DataGridViewColourComboBoxColumn : DataGridViewComboBoxColumn 
    { 
     public DataGridViewColourComboBoxColumn() 
     { 
      CellTemplate = new DataGridViewColourComboBoxCell(); 
      Name = "Colour"; 
      ValueMember = "Name"; 
     } 

     public override DataGridViewCell CellTemplate 
     { 
      get 
      { 
       return base.CellTemplate; 
      } 

      set 
      { 
       base.CellTemplate = value; 
      } 
     } 
    } 

    class DataGridViewColourComboBoxCell : DataGridViewComboBoxCell 
    { 
     public DataGridViewColourComboBoxCell() 
     { 
      InitialiseCell(); 
     } 

     private void InitialiseCell() 
     { 
      List<ushort> listColours = new List<ushort>(); 
      listColours.Add(1); 
      listColours.Add(2); 
      listColours.Add(3); 
      listColours.Add(4); 
      listColours.Add(5); 
      listColours.Add(6); 
      listColours.Add(7); 
      listColours.Add(8); 
      listColours.Add(9); 
      listColours.Add(250); 
      listColours.Add(251); 
      listColours.Add(252); 
      listColours.Add(253); 
      listColours.Add(254); 
      listColours.Add(255); 

      this.Items.Clear(); 
      foreach (ushort iColourIndex in listColours) 
       this.Items.Add(ComboboxColourItem.Create(iColourIndex)); 
     } 

     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, paintParts); 

      Color c = Color.Empty; 
      string s = ""; 
      Brush br = SystemBrushes.WindowText; 
      Brush brBack; 
      Rectangle rDraw; 

      rDraw = cellBounds; 
      rDraw = Rectangle.FromLTRB(cellBounds.Left, cellBounds.Top, cellBounds.Right, cellBounds.Bottom - 1); 

      brBack = Brushes.White; 
      //Pen penGridlines = new Pen(dataGridView.GridColor); 
      g.DrawRectangle(Pens.Black, rDraw); 
      g.FillRectangle(brBack, rDraw); 
      //penGridlines.Dispose(); 

      if (value != null) 
      { 
       ComboboxColourItem oColourItem = (ComboboxColourItem)value; 
       s = oColourItem.ToString(); 
       c = oColourItem.Value; 
      } 

      if (c != Color.Empty) 
      { 
       SolidBrush b = new SolidBrush(c); 
       Rectangle r = new Rectangle(cellBounds.Left + 6, 
              cellBounds.Top + 5, 20, 10); 
       g.FillRectangle(b, r); 
       g.DrawRectangle(Pens.Black, r); 
       g.DrawString(s, Form.DefaultFont, Brushes.Black, 
          cellBounds.Left + 35, cellBounds.Top + 3); 

       b.Dispose(); 
      } 

      int butSize = cellBounds.Height; 
      Rectangle rbut = new Rectangle(cellBounds.Right - butSize, 
              cellBounds.Top, butSize, butSize); 
      ComboBoxRenderer.DrawDropDownButton(graphics, rbut, 
         System.Windows.Forms.VisualStyles.ComboBoxState.Normal); 
     } 

     private void ColourComboBox_DrawItem(object sender, DrawItemEventArgs e) 
     { 
      Graphics g = e.Graphics; 
      Color c = Color.Empty; 
      string s = ""; 
      Brush br = SystemBrushes.WindowText; 
      Brush brBack; 
      Rectangle rDraw; 
      bool bSelected = Convert.ToBoolean(e.State & DrawItemState.Selected); 
      bool bValue = Convert.ToBoolean(e.State & DrawItemState.ComboBoxEdit); 

      rDraw = e.Bounds; 
      rDraw.Inflate(-1, -1); 

      if (bSelected & !bValue) 
      { 
       brBack = Brushes.LightBlue; 
       g.FillRectangle(Brushes.LightBlue, rDraw); 
       g.DrawRectangle(Pens.Blue, rDraw); 
      } 
      else 
      { 
       brBack = Brushes.White; 
       g.FillRectangle(brBack, e.Bounds); 
      } 

      try 
      { 
       ComboboxColourItem oColourItem = (ComboboxColourItem)((ComboBox)sender).Items[e.Index]; 
       s = oColourItem.ToString(); 
       c = oColourItem.Value; 
      } 
      catch 
      { 
       //s = "red"; 
       //c = Color.Red; 
      } 

      if (c != Color.Empty) 
      { 
       SolidBrush b = new SolidBrush(c); 
       Rectangle r = new Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 3, 20, 10); 
       g.FillRectangle(b, r); 
       g.DrawRectangle(Pens.Black, r); 
       g.DrawString(s, Form.DefaultFont, Brushes.Black, e.Bounds.Left + 35, e.Bounds.Top + 1); 

       b.Dispose(); 
      } 

      //g.Dispose(); 
     } 

    } 

    public class ComboboxColourItem 
    { 
     public string Name { get; set; } 
     public ushort Index { get; set; } 
     public Color Value { get; set; } 

     public ComboboxColourItem(string Name, ushort Index, Color Value) 
     { 
      this.Name = Name; 
      this.Index = Index; 
      this.Value = Value; 
     } 
     public override string ToString() 
     { 
      return Name; 
     } 

     static public ComboboxColourItem Create(ushort iColourIndex) 
     { 
      OdCmColor oColour = new OdCmColor(); 

      oColour.setColorIndex(iColourIndex); 

      CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture; 
      TextInfo textInfo = cultureInfo.TextInfo; 

      String strColour = textInfo.ToTitleCase(oColour.colorNameForDisplay()); 
      if (iColourIndex < 8) 
       strColour = String.Format("{0} ({1})", strColour, iColourIndex); 
      else if (iColourIndex == 8 || iColourIndex == 9 || iColourIndex >= 250) 
       strColour = String.Format("Grey Shade ({0})", iColourIndex); 
      else 
       strColour = String.Format("Other ({0})", iColourIndex); 
      ComboboxColourItem oColourItem = new ComboboxColourItem(
       strColour, 
       iColourIndex, 
       Color.FromArgb(oColour.red(), oColour.green(), oColour.blue())); 

      oColour.Dispose(); 

      return oColourItem; 
     } 
    } 
} 

Das Problem Ich habe einen der Moment ist dies:

private void DataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
{ 
    if (e.Control is ComboBox) 
    { 
     ComboBox theCB = (ComboBox)e.Control; 
     theCB.DrawMode = DrawMode.OwnerDrawFixed; 
     try 
     { 
      theCB.DrawItem -= new DrawItemEventHandler(this.combobox1_DrawItem); 
     } 
     catch { } 
     theCB.DrawItem += new DrawItemEventHandler(this.combobox1_DrawItem); 
    } 
} 

Oben ist der Handler des DataGridView-Objekts. Wie repliziere ich das in meiner DataGridViewColourComboBoxCell-Klasse? Es sollte diese Klasse sein, die die DrawItem-Methode festlegt.

Update:

Ich sehe Ich muss auch meine eigene Editiersteuerabschnitt und tun machen:

public override Type EditType 
{ 
    get 
    { 
     return typeof(DataGridViewColourComboBoxEditingControl); 
    } 
} 

Dann fügte ich:

protected override void OnDrawItem(DrawItemEventArgs e) 
    { 
     //base.OnDrawItem(e); 

     Graphics g = e.Graphics; 
     Color c = Color.Empty; 
     string s = ""; 
     Brush br = SystemBrushes.WindowText; 
     Brush brBack; 
     Rectangle rDraw; 
     bool bSelected = Convert.ToBoolean(e.State & DrawItemState.Selected); 
     bool bValue = Convert.ToBoolean(e.State & DrawItemState.ComboBoxEdit); 

     rDraw = e.Bounds; 
     rDraw.Inflate(-1, -1); 

     if (bSelected & !bValue) 
     { 
      brBack = Brushes.LightBlue; 
      g.FillRectangle(Brushes.LightBlue, rDraw); 
      g.DrawRectangle(Pens.Blue, rDraw); 
     } 
     else 
     { 
      brBack = Brushes.White; 
      g.FillRectangle(brBack, e.Bounds); 
     } 

     try 
     { 
      ComboboxColourItem oColourItem = (ComboboxColourItem)Items[e.Index]; 
      s = oColourItem.ToString(); 
      c = oColourItem.Value; 
     } 
     catch 
     { 
      //s = "red"; 
      //c = Color.Red; 
     } 

     if (c != Color.Empty) 
     { 
      SolidBrush b = new SolidBrush(c); 
      Rectangle r = new Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 3, 20, 10); 
      g.FillRectangle(b, r); 
      g.DrawRectangle(Pens.Black, r); 
      g.DrawString(s, Form.DefaultFont, Brushes.Black, e.Bounds.Left + 35, e.Bounds.Top + 1); 

      b.Dispose(); 
     } 
    } 
} 

So sind die Fragen, die ich jetzt noch für sind:

In der Zelle Malen Ereignis:

kann ich nicht tun:

//Pen penGridlines = new Pen(dataGridView.GridColor); 

Da wir in dem Zellobjekt sind.

Auch ist dies das Äquivalent von "CellParsing":

public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter) 
{ 
    return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter); 
} 
+0

Gefunden http://www.informit.com/articles/article.aspx?p=446453&seqNum=14 –

+0

Ich bezweifle, dass ein DGV eine seiner Zellen ein Paint-Ereignis aussetzen lassen wird. Aber natürlich können Sie ein eigenes Paint-Ereignis vom CellPainting-Ereignis aufrufen, indem Sie den DataGridViewCellPaintingEventArgs-Parameter weitergeben, damit der Code an die spezialisierten Spaltenklassen verteilt wird. – TaW

+0

Ich werde später schauen. Aber ich muss auch meine eigene Zellklasse ableiten, und ich denke, dass das Cell-Painting-Ereignis da ist, um es zu überschreiben. –

Antwort

1

Das half mir wirklich:

http://www.codeproject.com/Articles/15827/Gain-Access-To-DataGridView-Canned-Controls

ich aus dem DataGridViewComboBoxEditingControl eine neue abgeleitete Klasse zu schaffen hatte und in diesem Objekt als OwnerDraw setzen und das Ereignis OnDrawItem behandeln.

In der DataGridViewColourComboBoxCell Klasse hatte ich die EditType Eigenschaft außer Kraft zu setzen:

public override Type EditType 
{ 
    get 
    { 
     return typeof(DataGridViewColourComboBoxEditingControl); 
    } 
} 

Für die CellParsing ich in der Tat ParseFormattedValue außer Kraft zu setzen hatte.

+1

Sie sind in der richtigen Richtung, ich habe versucht, einen Beispielcode zu schreiben. Sie können sich auch [How to: Controls in Windows Forms-DataGridView-Zellen anzeigen] (https://msdn.microsoft.com/en-us/library/7tas5c80 (v = vs.110) .aspx? Cs-save ansehen -lang = 1 & cs-lang = csharp # code-snippet-1) –

+0

Ich habe ein Problem.Wenn ich auf das Drop-down-Feld für jede Zelle klicke, wird die Anzahl der Elemente multipliziert. –

+1

Ich habe 'InitialiseCell' stattdessen in die Spaltenklasse verschoben und im Konstruktor aufgerufen. Jetzt bekomme ich kein Vielfaches. –

Verwandte Themen