c# - 如何在不使用设计器的情况下向 Infragistics UltraGrid 添加列

标签 c# infragistics ultrawingrid

我在 Visual Studio 2015 中使用 Infragistics UltraGrid 设计器时遇到了麻烦,因为当我尝试在不绑定(bind)数据的情况下添加和定义列时,它会不断抛出错误。我什至明确选择不将数据绑定(bind)到它,但无论如何它都会抛出错误。一段时间后,我放弃了,现在我尝试在不依赖设计器的情况下在 C# 中手动添加列。

我试图找到不依赖设计器的在UltraGrid中添加列的方法,但没有找到有用的数据。我试过查看 Infragistics 2015 v2 Documentation对于“UltraGrid”和“UltraGrid Column”,他们没有任何关于在不依赖 UltraGrid 设计器的情况下创建列的内容。

有人知道如何在不依赖 UltraGrid 设计器的情况下向 UltraGrid 添加新列吗?

最佳答案

我想我已经自己找到了这个问题的答案,并且我认为如果我在这里发布我的发现,一些人会受益。

<强>1。首先...通过添加和定义列来制作标题

<强>2。接下来,添加数据

<强>3。最后,绑定(bind)数据。

//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;
    }
}

给你。 :-)

如果您遇到问题,请告诉我,我会尽力帮助您。

关于c# - 如何在不使用设计器的情况下向 Infragistics UltraGrid 添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37751994/

相关文章:

performance - 测量表格列中最长单元格的有效方法

javascript - Infragistics Ignite UI 2014.2 (14.2.20142.1024) 与 jquery 1.10.2 兼容吗?

.net - 如何在 UltraGrid 中查找当前选定(或事件)的行

c# - Ultragrid 未显示特定列的 ValueList

c# - WebView和错误管理,怎么了?

c# - MonoDroid、MonoTouch 通用 UI

c# - 涉及泛型和自继承的继承混淆

c# - 通过代理下载 PDF

asp.net - Infragistics WebHierarchicalDataGrid 无法确定何时选择子行以及如何获取所选的数据

winforms - 在 Infragistics UltraGrid 中为选定行上有错误的单元格设置背景