2013-03-28 52 views
0

我使用Infragistics UltraGrid,它的行爲與常規datagrid相似。DataGrid列更改爲空

我的網格看起來是這樣的:

------------------------- 
| Red | Blue | Green | 
------------------------- 
| null | null | null | 
------------------------- 

我建立一個DataTable將其指定爲DataSource我的網格。我DataTable

  • 5個紅色項目
  • 2個藍色物品

當我給你我的數據源是這樣的:
myGrid.DataSource = myDataTable或類似這樣的
myGrid.SetDataBinding(myTable, Nothing, True)

我得到這個電網:

------------------ 
| Red | Blue | 
------------------ 
| 5 | 2 | 
------------------ 

當我應該有這樣的:

--------------------------- 
| Red | Blue | Green | 
--------------------------- 
| 5 | 2 | null | 
---------------------------- 

有一些財產我失蹤,允許我的表調整到非空列?

+0

你確定你的數據表中有三列命名爲紅色,藍色和綠色網格? – Steve

+0

@Steve沒有我的數據表沒有綠色的列。但我的數據網格有三個(在設計器中),所以我想要做的是用數據表填充紅色和藍色表格列,但保持我的綠色列甚至艱難,它不在數據表中 – phadaphunk

回答

2

當您將DataSource屬性設置爲您的DataTable時,您將使用指定的表的架構信息替換通過Designer設置的架構信息。
據我所知,沒有辦法保留那個缺失的列。

但是你可以在缺少的列重新添加到使用InitializeLayout事件

Private Sub myGrid_InitializeLayout(sender As object, _ 
      e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) 
    Dim b As UltraGridBand = e.Layout.Bands[0]; 
    if Not b.Columns.Exists("RED") Then 
     Dim colRed As UltraGridColumn = b.Columns.Add("RED", "RED"); 
     colRed.NullText = "Null" 
     colRed.Header.VisiblePosition = 0 
    End if 
    if Not b.Columns.Exists("BLU") Then 
     Dim colBlu As UltraGridColumn = b.Columns.Add("BLU", "BLU"); 
     colBlu.NullText = "Null" 
     colBlu.Header.VisiblePosition = 1 
    End if 
    if Not b.Columns.Exists("GREEN") Then 
     Dim colGreen As UltraGridColumn = b.Columns.Add("GREEN", "GREEN"); 
     colGreen.NullText = "Null" 
     colGreen.Header.VisiblePosition = 2 
    End if 
End Sub 
+0

謝謝,我確信這是我會做什麼..! – phadaphunk

+0

Steve是正確的,因爲架構與您綁定的數據源不匹配,所以沒有任何方法可以保留設計器的架構信息。在這種情況下,一個簡單的解決方案是,如果列預期存在,那麼總是將「GREEN」列添加到DataTable中,如果您在設計器中添加列,那麼這將是我所假設的。 – alhalama

+0

@alhalama,是的,我同意你最好在綁定之前添加到數據表中。然而,關於OP問題的一些問題已經將我的想法推到了通過查詢獲得的表格中,其中也可能丟失了RED和BLU列。因此,我已經展示了UltraGrid處理 – Steve