2011-10-29 74 views
1

我有一段代碼,如下所示。我正在使用Microsoft Visual C#速成版。我的問題是,我無法在設計時將這些列添加到DataGridView中,這是另一個類的成員。這裏是我的代碼:DataGridView作爲屬性類型

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace ClassLibrary1 
{ 
    public class Class1 : Panel 
    { 
     DataGridView mView; 

     public Class1() 
     { 
     mView = new DataGridView(); 
     this.Controls.Add(mView); 
     } 

     public DataGridView View 
     { 
      get { return mView; } 
      set { mView = value; } 
     } 
    } 
} 

當我點擊三個點按鈕,在屬性窗口中添加新列在我設計時得到了System.NullException。由於我的聲譽,我無法發佈截圖。

感謝您的幫助!

+0

此答案會讓你關閉,但不夠接近。 .Net 2.0控件具有不支持嵌套的設計器。 http://stackoverflow.com/questions/2785376/how-to-bubble-a-controls-features-when-place-in-a-custom-usercontrol/2863807#2863807 –

回答

1

我只是深入瞭解你的問題。不幸的是,我能夠重現所描述的問題。在usercontrols的DataGridView中進行了一些研究後,我對以下的post產生了疑惑。

看起來有沒有可視化支持爲繼承DataGridView - 而不是爲userControls使用DataGridView,也。你也可以看看這個article

我試過使用下面的繼承DataGridView類 - 但都沒有工作。我很抱歉沒有一個適當的解決方案 - 但我希望這可能對你有任何用處。

/// tried this attribute - did not work 
/// [Designer(typeof (System.Windows.Forms.Design.ControlDesigner))] 

/// this did not work either 
[Editor("System.Windows.Forms.Design.DataGridViewComponentEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(ComponentEditor))] 
[Designer("System.Windows.Forms.Design.DataGridViewDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")] 
public class ucInheritedDataGridView : DataGridView { } 
+0

其實我試過甚至一個自定義的數據網格對象它使用自定義列集合繼承System.Windows.Forms.DataGridView。但它也沒有效果。第二個鏈接是我猜的答案。看來我必須現在編寫我自己的DataGridView!謝謝.. – Taha