使用DataTable.ExtendedProperties在网格中显示有限数量的列

时间:2014-08-01 13:34:54

标签: datatable extended-properties

我想在网格中显示有限数量的列,这些列连接到DataTable作为数据源。在我找到的示例中,它使用属性ExtendedProperties来定义列标题的显示方式,顺序以及选择的列。但是,当我使用它时,显示的列,顺序和数字与原始DataTable中的相同。

有人能看出我做错了吗?

这是代码的子集。这只是客户的表:

// initialize db connection variables
        string conn = GetConnectionString();

        // load some tables
        string[] tables = "Customers, Orders, Order Details, Products, Employees, Shippers".Split(',');
        foreach (string tableName in tables)
        {
            FillTable(_ds, tableName, conn);
        }
dt = _ds.Tables["Customers"];
// re-arrange the columns on the customer table
        //
        dt.ExtendedProperties.Add("ShowColumns", new string[] {
                                                                  "CustomerID, Customer",
                                                                  "OrderCount, Orders",
                                                                  "CompanyName, Company",
                                                                  "ContactName, Contact",
                                                                  "Phone",
                                                                  "City",
                                                                  "Region",
                                                                  "Country",
        });

        // show customers to begin with
        _flex.SetDataBinding(_ds, "Customers");

设置列的方法:

    // customize grid display to show selected columns, captions, formats, and data maps
    void _flex_SetupColumns(object sender, System.EventArgs e)
    {
        // get grid that was just bound
        C1FlexDataTree grid = sender as C1FlexDataTree;
        if (grid == null || grid.DataSource == null)
            return;

        // get source DataTable
        CurrencyManager cm = (CurrencyManager)BindingContext[grid.DataSource, grid.DataMember];
        DataTable dt = ((DataView)cm.List).Table;

        // apply custom column order, captions, format
        string[] columns = dt.ExtendedProperties["ShowColumns"] as string[];
        if (columns != null)
        {
            SetupColumns(grid, columns);
        }

        // apply custom data maps
        foreach (Column gridColumn in grid.Cols)
        {
            DataColumn dataColumn = dt.Columns[gridColumn.Name];
            if (dataColumn == null) continue;
            gridColumn.DataMap = dataColumn.ExtendedProperties["DataMap"] as IDictionary;
            if (gridColumn.DataMap != null)
            {
                gridColumn.TextAlign = TextAlignEnum.LeftCenter;
            }
        }

        // all done, autosize to show mapped data
        if (grid.AutoResize)
        {
            grid.AutoSizeCols(12);
        }
    }

方法,' SetupColumns'永远不会被称为。

1 个答案:

答案 0 :(得分:0)

我想出来了。我的问题是我没有在网格中定义事件,因此它没有被触发。一旦我这样做,它运作良好。