C# DataGridView 锁定在继承的 UserControl 上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/207504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridView locked on a inherited UserControl
提问by benPearce
I have a UserControl with some predefined controls (groupbox,button,datagridview) on it, these controls are marked as protected and the components variable is also marked as protected.
我有一个带有一些预定义控件(groupbox、button、datagridview)的 UserControl,这些控件被标记为受保护,组件变量也被标记为受保护。
I then want to inherit from this base UserControl to another UserControl, however the DataGridView is always locked in the designer.
然后我想从这个基 UserControl 继承到另一个 UserControl,但是 DataGridView 总是被锁定在设计器中。
I suspect it may have something to do with the DataGridView implementing ISupportInitilize.
我怀疑它可能与实现 ISupportInitilize 的 DataGridView 有关。
public class BaseGridDetail : UserControl
Has a DataGridView control (et al) defined.
定义了一个 DataGridView 控件(等)。
public class InheritedDetail : BaseGridDetail
The DataGridView control is locked
DataGridView 控件被锁定
Does anyone have any ideas how to make this control available in the designer after inheritenace?
有没有人知道如何在继承后在设计器中使用此控件?
采纳答案by RobS
By the looks of it, DataListView (and some other controls) do not support visual inheritance. There's a connect issue logged herewhich doesn't look hopeful.
从外观上看,DataListView(和一些其他控件)不支持视觉继承。这里记录了一个连接问题,看起来没有希望。
There have been similar issues logged with other form controls, e.g. flowlayoutpanel.
其他表单控件也记录了类似的问题,例如flowlayoutpanel。
I'm unable to find a way to force visual inheritance.
我无法找到强制视觉继承的方法。
Here's the official answer on connect: "For this particular release, the DataGridView was not designed to be used in visual intheritance. We will keep your suggestion in mind when we plan our future release" That is of 26/05/2006.
以下是关于connect的官方回答:“对于此特定版本,DataGridView 并非设计用于视觉继承。在我们计划未来版本时,我们会牢记您的建议” 那是 26/05/2006。
Update: found this blog post which may have the answer
更新:发现这篇博文可能有答案
Edit: I was unable to verify the blog post's claims. Looks like might be the latest on this issue
编辑:我无法验证博客文章的声明。看起来可能是这个问题的最新消息
It looks like you can still manipulate the DataListView at runtime though, so you might be able to set visual properties (and other settings). It's not a great compromise.
看起来您仍然可以在运行时操作 DataListView,因此您可以设置视觉属性(和其他设置)。这不是一个很好的妥协。
回答by Matt Hamilton
I left an answer but re-read your question and decided to delete it.
我留下了一个答案,但重新阅读了您的问题并决定将其删除。
What is it about the DataGridView that you're trying to modify in the inherited control? It's columns? I've been able to do this by setting up a protected method in my base UserControl and passing the grid's column collection into it, like this:
您试图在继承的控件中修改的 DataGridView 是什么?是柱子?我已经能够通过在我的基本 UserControl 中设置一个受保护的方法并将网格的列集合传递给它来做到这一点,如下所示:
// in base UserControl
public BaseGridDetail()
{
InitializeComponent();
InitGridColumns(dataGridView1.Columns);
}
protected virtual void InitGridColumns(DataGridViewColumnCollection columns)
{
columns.Clear();
}
Now your derived control can simply override that method, like so:
现在您的派生控件可以简单地覆盖该方法,如下所示:
// in InheritedDetail
protected override void InitGridColumns(DataGridViewColumnCollection columns)
{
base.InitGridColumns(columns);
// add my own custom columns
}
回答by Robocide
[1] create Your Custom UserControl
[1] 创建您的自定义用户控件
[2] make your custom userControl use the below Inherited DataGridView:
[2] 使您的自定义 userControl 使用以下 Inherited DataGridView:
[Designer(typeof System.Windows.Forms.Design.ControlDesigner))]
public class InheritedDataGridView : DataGridView { }
[3] Inherit from your Custom UserControl , And viola !!
[3] 从您的自定义 UserControl 继承,以及中提琴!!
[4] Ohh dont forget to add "System.Design" dll
[4] 哦,别忘了添加“System.Design”dll
Enjoy.
享受。
回答by John
Add the datagrid to a panel control and set the modifiers of the panel and the datagrid to protected. This will all your inherited form design time access to the properties of the grid.
将数据网格添加到面板控件并将面板和数据网格的修饰符设置为受保护。这将在您继承的所有表单设计时访问网格的属性。
回答by Na Youngmin
Change property to defined [private] to [protected] defined at xx.designer.cs which is originally machine generated code.
将属性更改为在 xx.designer.cs 中定义的定义 [private] 到 [protected],它最初是机器生成的代码。
for example
例如
private System.Windows.Forms.Button btnSave;
to
到
protected System.Windows.Forms.Button btnSave;
and rebuild it.
并重建它。
then you can change the property of inherited control.
然后您可以更改继承控件的属性。