2016-06-10 69 views
2

我在Visual Studio 2015中與Infragistics UltraGrid設計器一起奮鬥,因爲當我嘗試添加並定義列而沒有綁定數據時,它會一直拋出錯誤。我甚至明確地選擇不將數據綁定到它,但無論如何拋出錯誤。過了一段時間,我放棄了,現在我試圖在C#中手動添加列,而不依賴於設計人員。如何在不使用設計器的情況下向Infragistics UltraGrid添加列

我試圖找到在不依賴設計器的情況下在UltraGrid中添加列的方法,而且我沒有找到有用的數據。我曾嘗試在「UltraGrid」和「UltraGrid Column」中尋找Infragistics 2015 v2 Documentation,並且他們沒有任何關於創建列而不依賴於UltraGrid設計器的任何內容。

有誰知道如何在不依賴UltraGrid設計器的情況下向UltraGrid添加新列?

+0

你可以在後面的代碼中添加,就像創建UltraGrid的實例一樣。 – MethodMan

+0

我不知道我理解你的問題。如果你的問題是我可以通過創建一個UltraGrid的實例來完成,那麼我的答案將是我不知道如何。我嘗試通過Infragistics文檔進行挖掘,並且文檔沒有幫助。 – Sometowngeek

回答

2

我想我自己已經想出了這個問題的答案,並且我認爲如果我發佈了我在這裏發現的內容,它會使一些人受益。

1.首先...通過添加和定義列

2.接下來做頭,添加數據

3.最後,數據綁定。

//Be sure to include this heading: 
using Infragistics.Win.UltraWinGrid; 

public class ClassName{ 

    // 1. Make the Headers by adding and defining columns. 
    private static DataTable MakeTableHeaders() 
    { 
     DataTable myDataTable = new DataTable("My Table"); 
     // Declare variables for DataColumn and DataRow objects. 
     DataColumn column; 

     // Properties: 
     // column.DataType = set data type (System.Int32, System.String, etc...) 
     // column.ColumnName = set column key (it MUST be unique) (String) 
     // column.Caption = set the string to be visible for column header. (String) 
     // column.ReadOnly = set whether the column is editable or not. (Boolean) 
     // column.Unique  = set whether or not values in the column must be unique. 
     //      Unique values = each cell must be different each other. 
     //      (Boolean) 


     //// Program ID 
     //// Caption "ID" 
     column = new DataColumn(); 
     column.DataType = System.Type.GetType("System.Int32"); 
     column.ColumnName = "ID"; 
     column.Caption = "ID"; 
     column.ReadOnly = true; 
     column.Unique = true; 
     // Adds the column to the programTable. 
     myDataTable.Columns.Add(column); 

     //// Program Name 
     //// Caption "Program" 
     column = new DataColumn(); 
     column.DataType = System.Type.GetType("System.String"); 
     column.ColumnName = "Program"; 
     column.Caption = "Program"; 
     column.ReadOnly = true; 
     column.Unique = false; 
     // Adds the column to the programTable. 
     myDataTable.Columns.Add(column); 

     // Add the rest of the necessary columns. 
     // .... 

     // When completed, return the table. 
     return myDataTable; 
    } 

    // 2. Next, add data. 
    public static DataSet loadData() 
    { 
     DataTable myDataTable = MakeTableHeaders(); 

     // Add a new empty data row to our data model. 
     DataRow theDataRow = myDataTable.NewRow(); 

     // Add data 
     theDataRow[0] = 0; 
     theDataRow[1] = "Program Name"; 

     // Add the DataRow to the table. 
     myDataTable.Rows.Add(theDataRow); 

     // Don't forget to accept changes, 
     // or the data may not be retained. 
     myDataTable.AcceptChanges(); 

     // Create a new DataSet. 
     gridDataSet = new DataSet(); 

     // Add the table to DataSet. 
     gridDataSet.Tables.Add(myDataTable); 

     // Return the DataSet. 
     return gridDataSet; 
    } 

    // 3. Finally, bind data. 
    // Do it in the construct of your class 
    public ClassName() 
    { 
     // Use the UltraGrid name you assigned to 
     // your UltraGrid. 
     ugMyUltraGrid.DataSource = loadData(); 
    } 

    ugMyUltraGrid_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.CellEventArgs) 
    { 
     // This is used to place columns in specific place. 
     int headerPosition = 0; 

     // Set cell editability 
     // Activation.ActivateOnly = The context in the cell can be selected, but cannot be edited. 
     // Activation.AllowEdit = Allows the cell to be edited. 
     // Activation.Disabled = Disables the cell. 
     // Activation.NoEdit = Only allows the cell to be activated. 
     col.CellActivation = Activation.ActivateOnly; 

     // Hide all columns 
     col.Hidden = true; 

     // Program ID 
     column = "ID"; 
     ugColumn = e.Layout.Bands[parent].Columns[column]; 
     e.Layout.Bands[parent].Columns[column].Header.Caption = "ID"; 
     e.Layout.Bands[parent].Columns[column].Header.VisiblePosition = headerPosition++; 
     e.Layout.Bands[parent].Columns[column].Width = 50; 
     // To size it to a fixed column width, use this instead: 
     // e.Layout.Bands[parent].Columns[column].MinWidth = e.Layout.Bands[parent].Columns[column].MaxWidth = 50; 

     // Program Name 
     column = "Program"; 
     e.Layout.Bands[parent].Columns[column].Header.Caption = "Project"; 
     e.Layout.Bands[parent].Columns[column].Header.VisiblePosition = headerPosition++; 
     e.Layout.Bands[parent].Columns[column].Width = 150; 
     // To size it to a fixed column width, use this instead: 
     // e.Layout.Bands[parent].Columns[column].MinWidth = e.Layout.Bands[parent].Columns[column].MaxWidth = 150; 
    } 
} 

你有它。 :-)

讓我知道如果你遇到問題,那麼我會盡我所能來幫助你。

相關問題