2012-11-03 56 views
0

我從DHCP服務器的配置數據,我把成類是這樣的:嵌套類中的GridView

public class DHCP 
{ 
    public DHCP() 
    { 
     this.Scopes = new List<Scope>(); 
    } 

    public List<Scope> Scopes; 

    public class Scope 
    { 
     public Scope(string ScopeAddress, string SubnetMask, string State, string ScopeName, string Comment) 
     { 
      this.ScopeAddress = ScopeAddress; 
      this.SubnetMask = SubnetMask; 
      this.State = State; 
      this.ScopeName = ScopeName; 
      this.Comment = Comment; 
     } 

     public Scope(string ScopeAddress, string SubnetMask, string State, string ScopeName, string Comment, bool initClients) 
     { 
      this.ScopeAddress = ScopeAddress; 
      this.SubnetMask = SubnetMask; 
      this.State = State; 
      this.ScopeName = ScopeName; 
      this.Comment = Comment; 

      if (initClients) 
       this.Clients = new List<Client>(); 
     } 

     public void InitClients() 
     { 
      this.Clients = new List<Client>(); 
     } 

     public void InitReservations() 
     { 
      this.Reservations = new List<Reservation>(); 
     } 

     public string ScopeAddress { get; set; } 
     public string SubnetMask { get; set; } 
     public string State { get; set; } 
     public string ScopeName { get; set; } 
     public string Comment { get; set; } 

     public List<Client> Clients; 
     public List<Reservation> Reservations; 
    } 

    public class Client 
    { 
     public Client(string IPAddress, string SubnetMask, string UniqueID, string LeaseExpires, string ClientType) 
     { 
      this.IPAddress = IPAddress; 
      this.SubnetMask = SubnetMask; 
      this.UniqueID = UniqueID; 
      this.LeaseExpires = LeaseExpires; 
      this.ClientType = ClientType; 
     } 

     public string IPAddress { get; set; } 
     public string SubnetMask { get; set; } 
     public string UniqueID { get; set; } 
     public string LeaseExpires { get; set; } 
     public string ClientType { get; set; } 
     public Reservation ClientReservation { get; set; } 
    } 

    public class Reservation 
    { 
     public Reservation(string IPAddress, string UniqueID, bool ReservationActive) 
     { 
      this.IPAddress = IPAddress; 
      this.UniqueID = UniqueID; 
      this.ReservationActive = ReservationActive; 
     } 

     public string IPAddress { get; set; } 
     public string UniqueID { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
     public string Type { get; set; } 
     public bool ReservationActive { get; set; } 
    } 
} 

然後,我有一個List<DHCP.Client>,我在GridView上使用DataSource。當我將其中一個數據字段設置爲ClientReservation.ReservationActive時,我收到一個未找到的錯誤。

我試過這個列表<>這裏根本沒有NULL數據。 所以我必須問:

這可以完成嗎? 如果我有一個對象Client.ClientReservation == null我該如何處理DataBind()而不出錯?

回答

0

我找到了這兩個問題的解決方案。通過使用模板字段是這樣的:

<asp:TemplateField> 
    <ItemTemplate> 
    <%#DataBinder.Eval(Container.DataItem, "ClientReservation.ReservationActive")%> 
    </ItemTemplate> 
</asp:TemplateField> 

它得到的嵌套類(子屬性)的值,並忽略它沒有錯誤,如果ClientReservation爲NULL。

0

問題是,正如你可以看到你分配給所有的屬性在構造函數中的值除了ReservationActive。事實上,它仍然是空的,這就是爲什麼你會得到錯誤。所以,你應該做的:

public class Client 
{ 
    public Client(string IPAddress, string SubnetMask, string UniqueID, string LeaseExpires, string ClientType, Reservation ClientReservation) 
    { 
     this.IPAddress = IPAddress; 
     this.SubnetMask = SubnetMask; 
     this.UniqueID = UniqueID; 
     this.LeaseExpires = LeaseExpires; 
     this.ClientType = ClientType; 
     this.ClientReservation = ClientReservation; 
    } 

    public string IPAddress { get; set; } 
    public string SubnetMask { get; set; } 
    public string UniqueID { get; set; } 
    public string LeaseExpires { get; set; } 
    public string ClientType { get; set; } 
    public Reservation ClientReservation { get; set; } 
} 

與A通非空值。 否則你可以用這種方式進行初始化,而不使其通過構造函數:

Client client = new Client(); 
client.ClientReservation = new Reservation("127.0.0.1", "ID", true); //example 

然後你可以使用myClient.ClientReservation沒有得到任何錯誤。

編輯:

另一項建議。因爲你需要添加一些Client。你可以通過這種方式實現了這種可能性:

public Scope 
{ 
    //... 
    public void AddClient(string IPAddress, string SubnetMask, string UniqueID, string LeaseExpires, string ClientType, Reservation ClientReservation){ 
     Clients.Add(new Client(IPAddress, SubnetMask, UniqueID, LeaseExpires, ClientType, ClientReservation)); 
    } 
} 

然後:

Scopes = new List<Scope>(); 
Scope scope = new Scope(/*...*/); 
scope.AddClient(new Client(/*...*/)); 
Scopes.Add(scope); 
+0

是的我可以在代碼中使用myClient.ClientReservation,但不能作爲GridView控件中的綁定字段。這是我現在最大的問題。 –