System.Data.DataTable.ParseSortString C# (CSharp) Method

ParseSortString() private method

private ParseSortString ( string sortString ) : IndexField[]
sortString string
return IndexField[]
        internal IndexField[] ParseSortString(string sortString)
        {
            IndexField[] indexDesc = Array.Empty<IndexField>();
            if ((null != sortString) && (0 < sortString.Length))
            {
                string[] split = sortString.Split(new char[] { ',' });
                indexDesc = new IndexField[split.Length];

                for (int i = 0; i < split.Length; i++)
                {
                    string current = split[i].Trim();

                    // handle ASC and DESC.
                    int length = current.Length;
                    bool descending = false;
                    if (length >= 5 && string.Compare(current, length - 4, " ASC", 0, 4, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        current = current.Substring(0, length - 4).Trim();
                    }
                    else if (length >= 6 && string.Compare(current, length - 5, " DESC", 0, 5, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        descending = true;
                        current = current.Substring(0, length - 5).Trim();
                    }

                    // handle brackets.
                    if (current.StartsWith("[", StringComparison.Ordinal))
                    {
                        if (current.EndsWith("]", StringComparison.Ordinal))
                        {
                            current = current.Substring(1, current.Length - 2);
                        }
                        else
                        {
                            throw ExceptionBuilder.InvalidSortString(split[i]);
                        }
                    }

                    // find the column.
                    DataColumn column = Columns[current];
                    if (column == null)
                    {
                        throw ExceptionBuilder.ColumnOutOfRange(current);
                    }
                    indexDesc[i] = new IndexField(column, descending);
                }
            }
            return indexDesc;
        }

Usage Example

Example #1
0
        // This is method is internal to
        // the Mono implementation of DataView; it
        // is not to be used from your code.
        //
        // Update the DataRowView array which is an index cache
        // into the DataTable's DataRowCollection.
        //
        // I assume this is what UpdateIndex is used for
        protected virtual void UpdateIndex(bool force)
        {
            if (Table == null)
            {
                // FIXME
                return;
            }

            if (Index == null || force)
            {
                sortColumns = DataTable.ParseSortString(Table, Sort, out sortOrder, false);
                Index       = dataTable.GetIndex(sortColumns, sortOrder, RowStateFilter, FilterExpression, true);
            }
            else
            {
                Index.Key.RowStateFilter = RowStateFilter;
                Index.Reset();
            }

            int[] records = Index.GetAll();

            if (records != null)
            {
                InitDataRowViewArray(records, Index.Size);
            }
            else
            {
                rowCache = new DataRowView [0];
            }
        }
All Usage Examples Of System.Data.DataTable::ParseSortString
DataTable