一种方法是用 Hashtable 自己写 DataRowCollection,部分代码如下: public class DataRowCollectionEx : System.Collections.Hashtable
{
/// <summary>
/// 将带有指定键和值的元素添加到 Hashtable 中
/// </summary>
public void Add(string key, DataRow row)
{
base.Add (key, row);
}
/// <summary>
/// 索引器
/// </summary>
public DataRow this[string key]
{
get
{
return (DataRow) base[key];
}
set
{
base[key] = value;
}
} }
{
/// <summary>
/// 将带有指定键和值的元素添加到 Hashtable 中
/// </summary>
public void Add(string key, DataRow row)
{
base.Add (key, row);
}
/// <summary>
/// 索引器
/// </summary>
public DataRow this[string key]
{
get
{
return (DataRow) base[key];
}
set
{
base[key] = value;
}
} }
tmp.Add(键值,object);
{
// Methods
internal DataRowCollection(DataTable table);
public void Add(DataRow row);
public virtual DataRow Add(params object[] values);
internal void ArrayAdd(DataRow row);
internal void ArrayClear();
internal void ArrayInsert(DataRow row, int pos);
internal void ArrayRemove(DataRow row);
public void Clear();
public bool Contains(object[] keys);
public bool Contains(object key);
public void CopyTo(DataRow[] array, int index);
internal void DiffInsertAt(DataRow row, int pos);
public DataRow Find(object key);
public DataRow Find(object[] keys);
public int IndexOf(DataRow row);
public void InsertAt(DataRow row, int pos);
public void Remove(DataRow row);
public void RemoveAt(int index); // Properties
public DataRow this[int index] { get; }
protected override ArrayList List { get; } // Fields
private readonly ArrayList list;
internal int nullInList;
private readonly DataTable table;
}不过还不是很清楚。:-(
{
private string key = String.Empty;
public string Key
{
get { return key; }
set { key = value; }
}
}
public class DataRowColllectionEx2 : System.Data.DataRowCollection
{
public void Add(string key, DataRowEx row)
{
row.Key = key;
return base.Add (row);
}
/// <summary>
/// 索引器
/// </summary>
public DataRow this[string key]
{
get
{
return GetRowByKey(key);
}
set
{
// TODO: 设置元素值的逻辑未验证
DataRowEx row = GetRowByKey(key);
if( row != null )
row = value;
}
}
private DataRowEx GetRowByKey(string key)
{
foreach( DataRowEx row in this )
{
if( row.Key == key )
return row;
}
return null;
}
}
还有,搂主的信誉值够低的 =.=" 没别的意思,呵呵