[英]How to add columns to Infragistics UltraGrid without using designer
我在Visual Studio 2015中遇到了Infragistics UltraGrid設計器,因為當我嘗試添加和定義列而不綁定數據時,它一直在拋出錯誤。 我甚至明確地選擇不綁定數據,但無論如何它都會拋出錯誤。 過了一會兒,我放棄了,現在我正在嘗試手動添加C#中的列而不依賴於設計器。
我試圖找到在UltraGrid中添加列的方法而不依賴於設計器,我找不到有用的數據。 我曾嘗試在“UltraGrid”和“UltraGrid列”中查看Infragistics 2015 v2文檔 ,但他們沒有任何關於創建列的內容,而不依賴於UltraGrid設計器。
有沒有人知道如何在不依賴UltraGrid設計器的情況下向UltraGrid添加新列?
我想我自己已經找到了這個問題的答案,我認為如果我發布了我在這里發現的東西,它會使一些人受益。
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;
}
}
你有它。 :-)
如果您遇到問題,請告訴我,然后我會盡力幫助您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.