自定义DataGridView列 - 我哪里错了?

时间:2011-11-18 10:51:08

标签: c# datagridview

我需要创建一个允许以下内容的新DataGridView列类型:

  • 图像的多个小缩略图,如果需要可拆分为多行 是
  • 悬停或点击每个缩略图的功能,这将显示完整尺寸的图片
  • 将缩略图从一行拖到另一行的功能

一步一步,我试图让它至少正确地显示图像。这是我的代码:

public class DataGridViewPODColumn : DataGridViewColumn
{
    public DataGridViewPODColumn()
    {
         this.CellTemplate = new DataGridViewPODCell();
    }

    public override object Clone()
    {
         return base.Clone()
    }
}

这是细胞的代码:

public class DataGridViewPODCell : DataGridViewTextBoxCell
{
    private static Type defaultValueType = typeof(List<PODImage>);
    private int _XCoordinate = 0;
    private int _YCoordinate = 0;

    public int XCoordinate { get { return _XCoordinate; } set { _XCoordinate = value; } }
    public int YCoordinate { get { return _YCoordinate; } set { _YCoordinate = value; } }

    public override Type ValueType
    {
        get
        {
            Type valueType = base.ValueType;
            if (valueType != null) return valueType;
            return defaultValueType;
        }
    }

    protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
    {

        if (this.DataGridView == null) return;
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

        int counter = 0;
        foreach (PODImage podVal in (List<PODImage>)value)
        {
            GraphicsContainer container = graphics.BeginContainer();
            graphics.SetClip(cellBounds);
            Point objPoint = new Point(cellBounds.Location.X + XCoordinate, cellBounds.Location.Y + YCoordinate);
            graphics.DrawImage(podVal.POD, objPoint.X, objPoint.Y, 20, 40);
            XCoordinate += 22;
            counter++;
            if (counter % 3 == 0)
            {
                YCoordinate += 42;
                XCoordinate = 0;
            }
        }
    }

但是,我有一些重大问题:

  1. 我无法通过DGV设计器添加我的列 - 当我为列选择该类型并单击“添加”时,它只是不添加列,所以我必须在代码中执行此操作
  2. 大部分行变成黑色,单击图像会使其向右移动(即似乎认为它是新图像)
  3. 图片背后显示“收藏”一词
  4. 我知道这可能是相当基本的东西!我是Windows Forms开发的新手,过去一年只在ASP.NET中工作过......哦,我是如何渴望我的GridView和TemplateField的呢!

    如果有人能指出我正确的方向,我会非常感激。

0 个答案:

没有答案