11

Winforms .NET 3.5 (C#)

我有一个 DataGridView (DGView),并创建了要在 DGView 中显示的 CustomColumn 和 CustomCell。我创建了一个我想在 CustomCell 中显示的 CustomUserControl。

问题:我没有在列中看到用户控件。我想我需要在 CustomCell 中覆盖 Paint() 方法 - 我该怎么做?

注意 - 托管用户控件的 MSDN 示例用于编辑单元格值 - 您可以在其中使您的用户控件在您正在编辑单元格的位置可见。我希望我的用户控件呈现为普通的 winform 控件。此用户控件显示行的通知 .. 并且每行可以有不同的通知。我希望用户能够单击通知并获取有关它的更多详细信息。..但现在我被困在“如何显示这个用户控件”

任何指针将不胜感激。

 public class CustomColumn : DataGridViewColumn {
    public CustomColumn() : base(new CustomeCell()) { }
    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            // Ensure that the cell used for the template is a CalendarCell.
            if (value != null &&
                !value.GetType().IsAssignableFrom(typeof(CustomeCell)))
            {
                throw new InvalidCastException("It should be a custom Cell");
            }
            base.CellTemplate = value;
        }
    }
}
public class CustomeCell : DataGridViewTextBoxCell
{
    public CustomeCell() : base() { }
    public override Type ValueType
    {
        get
        {
            return typeof(CustomUserControl);
        }
    }
    public override Type FormattedValueType
    {
        get
        {
            return typeof(CustomUserControl);
        }
    }
}
4

2 回答 2

6

第一次尝试:我试图在我需要的网格上放置一个用户控件。问题:滚动数据网格视图需要重新排列所有这些用户控件。结果- 拒绝。

第二次尝试:我构建了一个用户控件并将其绘制在适当的单元格中。结果- 到目前为止有效。

我只是覆盖了类中PaintOnClick方法。DataGridViewCellCustomCell

public class CustomeCell : DataGridViewCell
{
    public override Type ValueType
    {
        get { return typeof(CustomUserControl); } 
    }

    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)
    {
        var ctrl = (CustomUserControl) value;
        var img = new Bitmap(cellBounds.Width, cellBounds.Height);
        ctrl.DrawToBitmap(img, new Rectangle(0, 0, ctrl.Width, ctrl.Height));
        graphics.DrawImage(img, cellBounds.Location);
    }

    protected override void OnClick(DataGridViewCellEventArgs e)
    {
        List<InfoObject> objs = DataGridView.DataSource as List<InfoObject>;
        if (objs == null)
            return;
        if (e.RowIndex < 0 || e.RowIndex >= objs.Count)
            return;

        CustomUserControl ctrl = objs[e.RowIndex].Ctrl;
        // Take any action - I will just change the color for now.
        ctrl.BackColor = Color.Red;
        ctrl.Refresh();
        DataGridView.InvalidateCell(e.ColumnIndex, e.RowIndex);
    }
}

该示例CustomControl在;)CustomCell中呈现。CustomColumn当用户点击单元格时,CustomCell'OnClick处理点击。理想情况下,我想将该点击委托给自定义用户控件CustomControl——它应该处理事件,就好像它是对自身的点击(自定义用户控件可以在内部托管多个控件)——所以它在那里并不复杂。

public class CustomColumn : DataGridViewColumn
{
    public CustomColumn() : base(new CustomeCell()) { }

    public override DataGridViewCell CellTemplate
    {
        get { return base.CellTemplate; }
        set
        {
            if (value != null && !value.GetType()
                .IsAssignableFrom(typeof (CustomeCell)))
                throw new InvalidCastException("It should be a custom Cell");
            base.CellTemplate = value;
        }
    }
}
于 2012-06-07T15:46:26.840 回答
1

DataGridView 控件仅支持在单元格处于编辑模式时显示实际控件。DataGridView 控件不是为显示多个控件或每行重复一组控件而设计的。DataGridView 控件在未编辑单元格时绘制控件的表示形式。该表示可以根据需要进行详细说明。例如,无论单元格是否处于编辑状态,DataGridViewButtonCell 都会绘制一个按钮。

但是,您可以通过 DataGridView.Controls.Add() 方法添加控件,并设置它们的位置和大小以使它们托管在单元格中,but showing controls in all cells regardless of editing make no sense.

在这里阅读

[更新 - 来自 MS DataGridView 团队的 Prog Mgr]

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/394e04a8-d918-4fdd-be03-dfa13f6a1e66?persist=True

于 2012-06-06T21:02:30.823 回答