在DatagridViewCell中显示用户控件

Winforms .NET 3.5(C#)

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

问题:我没有在列中看到用户控件。 我想我需要在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); } } } 

首先尝试 :我尝试将用户控件放在我需要的网格上。 问题:滚动数据网格视图需要重新安排所有这些用户控件。 结果 – 拒绝。

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

我只是在CustomCell类中覆盖了DataGridViewCell PaintOnClick方法。

 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 objs = DataGridView.DataSource as List; 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); } } 

该示例在CustomControlCustomCell中呈现CustomColumn ;)。 当用户单击单元格时, CustomCellOnClick处理单击。 理想情况下,我想将该单击委托给自定义用户控件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; } } } 

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

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

在这里阅读

[更新 – 来自MS DataGridView团队的Prog经理]

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