0

我想在连接到作为数据源的 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”方法。

4

1 回答 1

0

我想通了。我的问题是我没有在网格中定义事件,所以它没有触发。一旦我这样做了,它工作得很好。

于 2014-08-01T17:42:22.717 回答