2012-12-05 64 views
0

我有一個DataGrid,出於某種原因我必須聲明爲全局的。在第一次使用時,一切看起來都很順利。但是當我回到表單進行另一次嘗試時,會調用一個處理異常的對象。無論如何對我來說,以防止這種情況?像處理公共數據網格或其他東西?這裏是我的代碼示例:DataGrid上的對象處置異常

public static DataGrid dataGrid = new DataGrid(); 
public myForm() 
{ 
InitializeComponent(); 
dataGrid.Location = pt; 
dataGrid.Font.Name = "Tahoma"; 
dataGrid.Font.Size = 9; 
dataGrid.BackgroundColor = Color.Azure; 
dataGrid.GridLineColor = Color.Black; 
dataGrid.ColumnHeadersVisible = false; 
dataGrid.RowHeadersVisible = false; 
dataGrid.PreferredRowHeight = 60; 
this.Controls.Add(dataGrid); 
dataGrid.Height = 524; 
dataGrid.Width = 468; 
dataGrid.CurrentCellChanged += new 
EventHandler(dataGrid_CurrentCellChanged); 
} 
+0

提供你想要實現什麼的更多細節? –

+0

你從哪裏得到例外? –

+0

dataGrid.Location = pt發生異常。 如果我第二次使用表單,會發生這種情況。 –

回答

1

一個Form(或任何Control)它被設置在處置其子控件。所以你看到的是正常的。

爲了實現你想要的,你需要從表單控件集合中刪除DataGrid,然後再處理它。

UPDATE

由於@ctacke在評論中說,幾乎肯定的替代品,這將避免你的需要,使DataGrid靜態的,但沒有更詳細,很難提出建議。

+0

感謝您的建議。但是,讓我克服這個錯誤的方法是在表單中每次初始化時重新創建dataGrid。林不知道這是否安全。因此,而不是公共靜態DataGrid dataGrid = new DataGrid();我只是把公共靜態DataGrid的dataGrid;並在裏面初始化我把dataGrid = new DataGrid(); –

+0

@jeraldov - 當然會起作用,但是想讓DataGrid變爲靜態(「......出於某種原因,我必須聲明爲全局」)似乎很奇怪。 – Joe

+0

因爲我有一個類來檢查我的datagrid的內容並更改行顏色。 –

0

如果你要有一個靜態控件,至少要提供一個包裝器,以便你可以捕捉和處理你的問題。

考慮將您的代碼修改爲如下所示。一旦你解決了錯誤,你可以消除你不需要的任何東西。

private static DataGrid dataGrid; 
private static myForm myInstance; 

public myForm() 
{ 
    InitializeComponent(); 
    myInstance = this; // set 'myInstance' before DataGrid1 stuff 
    DataGrid1.Height = 524; 
    DataGrid1.Width = 468; 
    DataGrid1.CurrentCellChanged += new EventHandler(dataGrid_CurrentCellChanged); 
} 

public static DataGrid DataGrid1 { 
    get { 
    try { 
     if ((myInstance == null) || myInstance.IsDisposed) { 
     throw new Exception("myForm is already disposed. No controls available."); 
     } 
     if ((dataGrid == null) || dataGrid.IsDisposed) { 
     dataGrid = new DataGrid(); 
     dataGrid.Location = pt; 
     dataGrid.Font.Name = "Tahoma"; 
     dataGrid.Font.Size = 9; 
     dataGrid.BackgroundColor = Color.Azure; 
     dataGrid.GridLineColor = Color.Black; 
     dataGrid.ColumnHeadersVisible = false; 
     dataGrid.RowHeadersVisible = false; 
     dataGrid.PreferredRowHeight = 60; 
     this.Controls.Add(dataGrid); 
     } 
    } catch (Exception err) { 
     Console.WriteLine(err); // put a breakpoint HERE 
    } 
    return dataGrid; 
    } 
    set { 
    try { 
     if ((myInstance == null) || myInstance.IsDisposed) { 
     throw new Exception("myForm is already disposed. No controls available."); 
     } 
     if ((dataGrid == null) || dataGrid.IsDisposed) { 
     dataGrid = new DataGrid(); 
     dataGrid.Location = pt; 
     dataGrid.Font.Name = "Tahoma"; 
     dataGrid.Font.Size = 9; 
     dataGrid.BackgroundColor = Color.Azure; 
     dataGrid.GridLineColor = Color.Black; 
     dataGrid.ColumnHeadersVisible = false; 
     dataGrid.RowHeadersVisible = false; 
     dataGrid.PreferredRowHeight = 60; 
     this.Controls.Add(dataGrid); 
     } 
    } catch (Exception err) { 
     Console.WriteLine(err); // put a breakpoint HERE 
    } 
    dataGrid = value; 
    } 
} 

最後,確保你的dataGrid_CurrentCellChanged事件處理程序(和其他一切在你的程序)引用此公共DataGrid1對象,而不是dataGrid - 或者你會發現有自己一遍那些同樣的錯誤。