private bool InitResourceGridRows(EmbeddedResource er)
        {
            ResourceReader rr;
            try
            {
                rr = new ResourceReader(er.GetResourceStream());
            }
            catch
            {
                rr = null;
            }

            if (rr == null)
                return false;

            DataRow dr;
            IDictionaryEnumerator de = rr.GetEnumerator();
            int count = 0;
            while (de.MoveNext())
            {
                dr = _dtResource.NewRow();
                string name = de.Key as string;
                dr["no"] = count.ToString();
                count++;
                dr["name"] = name;
                object value = de.Value;
                dr["type"] = value == null ? null : value.GetType().FullName;
                try
                {
                    DataGridViewTextAndImageCellValue cv = new DataGridViewTextAndImageCellValue(name, value);
                    cv.Image = ConvertToImage(name, value);
                    dr["value"] = cv;
                }
                catch //(Exception ex)
                {
                    dr["value"] = new DataGridViewTextAndImageCellValue(String.Empty, value);
                    //dr["value"] = new DataGridViewTextAndImageCellValue(String.Empty, String.Format("Error: {0}", ex.Message));
                    //dr["type"] = "System.String";
                }
                _dtResource.Rows.Add(dr);
            }

            rr.Close();
            return true;
        }
Example #2
0
        protected override void Paint(Graphics graphics, Rectangle clipBounds,
                                      Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
                                      object value, object formattedValue, string errorText,
                                      DataGridViewCellStyle cellStyle,
                                      DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                      DataGridViewPaintParts paintParts)
        {
            Image image = null;
            DataGridViewTextAndImageCellValue cv = value as DataGridViewTextAndImageCellValue;

            if (cv != null)
            {
                image = cv.Image;
            }

            if (image != null && image.Width > 0)
            {
                //this will trigger Clone event because of SharedRow
                DataGridViewRow row = this.DataGridView.Rows[rowIndex];

                //use Bitmap to draw out proper size
                Bitmap bmp = new Bitmap(image);

                int height;
                int width;
                if (bmp.Width > cellBounds.Width)
                {
                    width  = cellBounds.Width;
                    height = Convert.ToInt32(1.0 * image.Height * cellBounds.Width / image.Width);
                }
                else
                {
                    width  = bmp.Width;
                    height = bmp.Height;
                }

                if (row.Height < height)
                {
                    row.Height = height;
                }

                base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
                           null, "", errorText, cellStyle,
                           advancedBorderStyle, paintParts);

                // Draw the image to the cell.
                GraphicsContainer container = graphics.BeginContainer();

                //graphics.SetClip(cellBounds);
                //graphics.DrawImageUnscaled(image, cellBounds.Location);
                Rectangle rect = new Rectangle(cellBounds.X, cellBounds.Y, width, height);
                graphics.DrawImage(bmp, rect);

                graphics.EndContainer(container);
            }
            else
            {
                // Paint the base content
                base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
                           value, formattedValue, errorText, cellStyle,
                           advancedBorderStyle, paintParts);
            }
        }