在DataGridViewCell的显示用户控制 [英] Display user control in DatagridViewCell

查看:170
本文介绍了在DataGridViewCell的显示用户控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的WinForms .NET 3.5(C#)



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



问题:我看不出在列的用户控件。我想我需要重写paint()方法中CustomCell - 任何点,我该怎么办呢?



请注意 - 托管用户控件的MSDN例子是编辑单元格的值 - 在你做出你在哪里编辑单元用户控件可见权。我希望我的用户控件呈现为一个正常的WinForm控件。这个用户控件显示了该行的通知..并且每行可以有不同的通知。我希望用户能够点击的通知,并得到有关它的更多细节。 ..但现在我停留在我怎么显示该用户控件



任何指针将高度赞赏。

 公共类CustomColumn:的DataGridViewColumn {
公共CustomColumn():基地(新CustomeCell()){}
公共重写的DataGridViewCell CellTemplate
{
得到
{
返回base.CellTemplate;
}
设置
{
//确保用于模板的细胞是CalendarCell。
如果(值=空&放大器;!&安培;!
value.GetType()IsAssignableFrom(typeof运算(CustomeCell)))
{
抛出新InvalidCastException的(这应该是自定义单元格);
}
base.CellTemplate =价值;
}
}
}
公共类CustomeCell:DataGridViewTextBoxCell
{
公共CustomeCell():基地(){}
公众覆盖类型值类型
{
得到
{
返回的typeof(CustomUserControl);
}
}
公众覆盖类型FormattedValueType
{
得到
{
返回的typeof(CustomUserControl);
}
}
}


解决方案

第一次尝试:我试着放在地方,我需要电网的用户控件。问题:滚动数据网格视图需要重新安排所有的用户控件。 结果 - 拒绝



第二个try :我构建了一个用户控制它画在相应的单元格。 结果 - 工程至今



我只是推翻油漆和<$ C $。 C>的OnClick >在 CustomCell 的DataGridViewCell

 公共类CustomeCell:DataGridViewCell的
{
公众覆盖类型的ValueType
{
{返回的typeof( CustomUserControl); }
}

保护覆盖无效油漆(图形图像,矩形clipBounds,矩形cellBounds,诠释的rowIndex,DataGridViewElementStates cellState,对象的值,对象FormattedValue的,串ERRORTEXT,的DataGridViewCellStyle cellStyle,DataGridViewAdvancedBorderStyle advancedBorderStyle,DataGridViewPaintParts paintParts)
{
VAR CTRL =(CustomUserControl)值;
变种IMG =新位图(cellBounds.Width,cellBounds.Height);
ctrl.DrawToBitmap(IMG,新的Rectangle(0,0,ctrl.Width,ctrl.Height));
graphics.DrawImage(IMG,cellBounds.Location);
}

保护覆盖无效的OnClick(DataGridViewCellEventArgs E)
{
名单,LT;&遍历infoObject GT; OBJ文件= DataGridView.DataSource的名单,LT;遍历infoObject取代;
如果(OBJ文件== NULL)
的回报;
如果(e.RowIndex℃,|| e.RowIndex> = objs.Count)
的回报;

CustomUserControl CTRL = OBJ文件[e.RowIndex] .Ctrl;
//采取任何行动 - 我将只是改变颜色现在。
ctrl.BackColor = Color.Red;
ctrl.Refresh();
DataGridView.InvalidateCell(e.ColumnIndex,e.RowIndex);
}
}

的例子呈现 CustomControl CustomCell CustomColumn 的)。当用户点击该单元格, CustomCell 的OnClick 处理点击。理想情况下,我想委派点击自定义用户控件 CustomControl - 这应该处理该事件,如果它是一个点击本身(自定义用户控件可以在内部托管多个控制) - 因此它有点复杂那里。

 公共类CustomColumn:的DataGridViewColumn 
{
公共CustomColumn():基地(新CustomeCell()) {}

公共重写的DataGridViewCell CellTemplate
{
{返回base.CellTemplate; }

{
如果(值=空&放大器;!&安培;!value.GetType()
.IsAssignableFrom(typeof运算(CustomeCell)))
抛出新InvalidCastException的(这应该是一个自定义单元格);
base.CellTemplate =价值;
}
}
}


Winforms .NET 3.5 (C#)

I have a DataGridView (DGView) and I created CustomColumn and CustomCell to be displayed in the DGView. I created a CustomUserControl which I want to display in the CustomCell.

Problem: I don't see the user control in the column. I think I need to override Paint() method in CustomCell - Any points how can I do that ?

Note - MSDN example of hosting user control is for editing the cell value - where you make your user control visible right where you are editing your cell. I want my user control to render as a normal winform control. This user control shows notifications for the row .. and each row can have different notifications. I want users to be able to click on notification and get more details about it. .. but for now I am stuck at "how do I display this user control"

Any pointers will be highly appreciated.

 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);
        }
    }
}

解决方案

First Try: I tried to place a user control on the grid where I needed. Problem: Scrolling the data grid view requires re arranging all those user controls. Result - Rejected.

Second Try: I constructed a user control and painted it in the appropriate cell. Result - works so far.

I just overrode Paint and OnClick methods of DataGridViewCell in the CustomCell class.

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);
    }
}

The example renders the CustomControl in the CustomCell of CustomColumn ;). When user clicks on the cell, CustomCell's OnClick handles the click. Ideally, I would like to delegate that click to the custom user control CustomControl - which should handle the event as if it was a click on itself (custom user control can internally host multiple controls) - so its little complex there.

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;
        }
    }
}

这篇关于在DataGridViewCell的显示用户控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆