DataGridView locked on a inherited UserControl

前端 未结 5 1361
慢半拍i
慢半拍i 2020-12-03 14:45

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

相关标签:
5条回答
  • 2020-12-03 15:21

    Change property to defined [private] to [protected] defined at xx.designer.cs which is originally machine generated code.

    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.

    0 讨论(0)
  • 2020-12-03 15:24

    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.

    0 讨论(0)
  • 2020-12-03 15:29

    [1] create Your Custom UserControl

    [2] make your custom userControl use the below Inherited DataGridView:

    [Designer(typeof System.Windows.Forms.Design.ControlDesigner))]
    public class InheritedDataGridView : DataGridView { }
    

    [3] Inherit from your Custom UserControl , And viola !!

    [4] Ohh dont forget to add "System.Design" dll

    Enjoy.

    0 讨论(0)
  • 2020-12-03 15:43

    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:

    // 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
    }
    
    0 讨论(0)
  • 2020-12-03 15:48

    By the looks of it, DataListView (and some other controls) do not support visual inheritance. There's a connect issue logged here which doesn't look hopeful.

    There have been similar issues logged with other form controls, e.g. 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.

    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.

    0 讨论(0)
提交回复
热议问题