我想在连接到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'从未被调用。
我明白了。我的问题是我没有在网格中定义Event,所以它没有触发。一旦我这样做了,它工作得很好。