Example #1
0
 private void SetColor(Color c)
 {
     BackColor = c;
     ForeColor = GraphicUtils.GetInvertedBlackWhite(c);
     if (FTextDisplayed)
     {
         Text = c.Name;
     }
     else
     {
         Text = "";
     }
 }
        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)
        {
            //            formattedValue = null;
            bool    painted = false;
            Variant nvalue  = (Variant)GetColumnValue("VALUE", rowIndex);

            if (!nvalue.IsNull)
            {
                ObjectInspectorCellType celltype = (ObjectInspectorCellType)GetColumnValue("TYPEENUM", rowIndex);
                if (celltype == ObjectInspectorCellType.Image)
                {
                    nvalue = (Variant)GetColumnValue("VALUEBIN", rowIndex);
                }
                switch (celltype)
                {
                case ObjectInspectorCellType.DropDownList:
                case ObjectInspectorCellType.DropDown:
                    Strings list  = (Strings)GetColumnValue("VALUELIST", rowIndex);
                    int     index = -1;
                    if (nvalue.IsString())
                    {
                        index = list.IndexOf(nvalue);
                    }
                    else
                    {
                        index = nvalue;
                    }
                    if (index < list.Count)
                    {
                        if (index >= 0)
                        {
                            formattedValue = list[index];
                        }
                        else
                        {
                            formattedValue = "";
                        }
                    }
                    break;

                case ObjectInspectorCellType.Text:
                    break;

                case ObjectInspectorCellType.Decimal:
                    Decimal dvalue = nvalue;
                    formattedValue = dvalue.ToString("##,##0.000");
                    break;

                case ObjectInspectorCellType.Integer:
                    Decimal ivalue = nvalue;
                    formattedValue = ivalue.ToString("##,##0");
                    break;

                case ObjectInspectorCellType.Color:
                    Color c = GraphicUtils.ColorFromInteger(nvalue);
                    formattedValue      = c.Name;
                    cellStyle.BackColor = c;
                    cellStyle.ForeColor = GraphicUtils.GetInvertedBlackWhite(c);
                    break;

                case ObjectInspectorCellType.Image:
                    System.IO.MemoryStream memstream = nvalue.GetStream();
                    formattedValue = StringUtil.GetSizeAsString(memstream.Length);
                    break;

                case ObjectInspectorCellType.FontStyle:
                    formattedValue      = GraphicUtils.StringFontStyleFromInteger(nvalue);
                    cellStyle.BackColor = SystemColors.Info;
                    cellStyle.ForeColor = SystemColors.InfoText;
                    break;

                case ObjectInspectorCellType.Boolean:
                    bool        boolvalue = (bool)nvalue;
                    ButtonState nstate;
                    if (boolvalue)
                    {
                        nstate = ButtonState.Checked;
                    }
                    else
                    {
                        nstate = ButtonState.Normal;
                    }
                    base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, "", "",
                               "", cellStyle, advancedBorderStyle, paintParts);
                    // Make square cell Bounds
//                        int checkwidth=(int)Math.Round((double)cellStyle.Font.Size*(double)96/(double)72);
                    int checkwidth = 13;
                    int x1         = cellBounds.Left;
                    int y1         = cellBounds.Top;
                    int width1     = cellBounds.Width;
                    int height1    = cellBounds.Height;
                    if (height1 > width1)
                    {
                        y1      = height1 - (checkwidth / 2);
                        height1 = width1;
                    }
                    else
                    {
                        x1     = x1 + width1 / 2 - (checkwidth / 2);
                        y1     = y1 + (height1 - checkwidth) / 2;
                        width1 = height1;
                    }
                    Rectangle nbounds = new Rectangle(x1, y1, checkwidth, checkwidth);
                    ControlPaint.DrawCheckBox(graphics, nbounds, nstate);
                    painted = true;
                    break;
                }
            }
            if (!painted)
            {
                base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue,
                           errorText, cellStyle, advancedBorderStyle, paintParts);
            }


            /*           Rectangle ColorBoxRect = new Rectangle();
             *         RectangleF TextBoxRect = new RectangleF();
             *         GetDisplayLayout(cellBounds, ref ColorBoxRect, ref TextBoxRect);
             *
             *         //// Draw the cell background, if specified.
             *         if ((paintParts & DataGridViewPaintParts.Background) ==
             *             DataGridViewPaintParts.Background)
             *         {
             *             SolidBrush cellBackground;
             *             if (value != null && value.GetType() == typeof(Color))
             *             {
             *                 cellBackground = new SolidBrush((Color)value);
             *             }
             *             else
             *             {
             *                 cellBackground = new SolidBrush(cellStyle.BackColor);
             *             }
             *             graphics.FillRectangle(cellBackground, ColorBoxRect);
             *             graphics.DrawRectangle(Pens.Black, ColorBoxRect);
             *             Color lclcolor = (Color)value;
             *             graphics.DrawString(lclcolor.Name.ToString(), cellStyle.Font, System.Drawing.Brushes.Black, TextBoxRect);
             *
             *             cellBackground.Dispose();
             *         }*/
        }