示例#1
0
        /// <summary>
        /// Gets the row containing the specified primary key values by searching the rows
        /// filtered by the state.
        /// </summary>
        internal DataRow Find(object [] keys, DataViewRowState rowStateFilter)
        {
            if (table.PrimaryKey.Length == 0)
            {
                throw new MissingPrimaryKeyException("Table doesn't have a primary key.");
            }

            if (keys == null)
            {
                throw new ArgumentException("Expecting " + table.PrimaryKey.Length + " value(s) for the key being indexed, but received 0 value(s).");
            }

            Index index  = table.GetIndex(table.PrimaryKey, null, rowStateFilter, null, false);
            int   record = index.Find(keys);

            if (record != -1 || !table._duringDataLoad)
            {
                return(record != -1 ? table.RecordCache [record] : null);
            }

            // If the key is not found using Index *and* if DataTable is under BeginLoadData
            // then, check all the DataRows for the key
            record = table.RecordCache.NewRecord();
            try {
                for (int i = 0; i < table.PrimaryKey.Length; ++i)
                {
                    table.PrimaryKey [i].DataContainer [record] = keys [i];
                }

                bool found;
                foreach (DataRow row in this)
                {
                    int rowIndex = Key.GetRecord(row, rowStateFilter);
                    if (rowIndex == -1)
                    {
                        continue;
                    }

                    found = true;
                    for (int columnCnt = 0; columnCnt < table.PrimaryKey.Length; ++columnCnt)
                    {
                        if (table.PrimaryKey [columnCnt].CompareValues(rowIndex, record) == 0)
                        {
                            continue;
                        }
                        found = false;
                        break;
                    }
                    if (found)
                    {
                        return(row);
                    }
                }
                return(null);
            } finally {
                table.RecordCache.DisposeRecord(record);
            }
        }