【发布时间】:2012-06-06 19:07:54
【问题描述】:
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);
}
}
}
【问题讨论】:
-
您还必须定义一个从 Control 派生并实现 IDataGridViewEditingControl 接口的类。
-
@AngshumanAgarwal 正如我在问题中提到的 - 我不想编辑,我只想在一列中显示所有行的用户控件。
-
我不太明白..但是您想向用户显示 datagridview 的未显示单元格的值?? (即当用户在单元格上单击一次时)如果是这样,那么只需将 ToolTip 与 SQLQuery 或 LINQ 一起使用