一种方法是用 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;
}
} }

解决方案 »

  1.   

    HashTable:System.Collections.Hashtable tmp =new Hashtable();
                tmp.Add(键值,object);
      

  2.   

    感谢各位:我查了一下文档,看起来是由DataRowCollection来的,public class DataRowCollection : InternalDataCollectionBase
    {
          // 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;
    }不过还不是很清楚。:-(
      

  3.   

    另外一种方法是给 DataRow 加一个 Key 属性,部分代码如下: public class DataRowEx : System.Data.DataRow
    {
    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;
    }
    }
      

  4.   

    主要的思路还是用 Hashtable 会比较好,因为 Hashtable 的索引器算法性能较好。而后面的 GetRowByKey 的遍历比较费时。BTW: 代码要自己去想过,写过,那才是自己的东西。一味的“重用”(Copy)是没有意义的。
    还有,搂主的信誉值够低的 =.=" 没别的意思,呵呵
      

  5.   

    谢谢ET2004兄。因为实在摸不着头脑,所以想拿代码来看看。BTW,我也不知为什么信誉值这么低。所有贴子我都结了,也没有发表反动言论。实在搞不清。