C#DataTable有一个PropertyCollection ExtendedProperties
。该表中的DataColumn也有ExtendedProperties
为什么DataRow没有这个?
因此,例如,如果我有多个表并想要添加一些要在视图中使用的元数据,我可以这样做:
tbl.ExtendedProperties["class"] = "pandas";
tbl.Columns["name"].ExtendedProperties["class"] = "highlighted";
我怎样才能进一步提高水平,做一些像
这样的事情tbl.Rows[0].ExtendedProperties["class"] = "highlighted";
答案 0 :(得分:1)
您可以尝试创建DataRow和DataTable的派生版本
[Serializable]
public class CustomDataTable : DataTable
{
public CustomDataTable()
: base()
{
}
public CustomDataTable(string tableName)
: base(tableName)
{
}
public CustomDataTable(string tableName, string tableNamespace)
: base(tableName, tableNamespace)
{
}
protected override Type GetRowType()
{
return typeof (CustomDataRow);
}
protected override DataRow NewRowFromBuilder(DataRowBuilder builder)
{
return new CustomDataRow(builder);
}
}
[Serializable]
public class CustomDataRow : DataRow
{
public Dictionary<string, object> _extendedProperties = new Dictionary<string, object>();
public Dictionary<string, object> ExtendedProperties {
get { return _extendedProperties; }
}
public void SetAttribute(string name, object value)
{
ExtendedProperties.Add(name, value);
}
public CustomDataRow()
: base(null)
{
}
public CustomDataRow(DataRowBuilder builder)
: base(builder)
{
}
}