OrderBy命名索引

时间:2012-10-19 09:48:31

标签: c# linq sorting datagridview

我有一个DataGridView,它保存从数据库中的存储过程返回的数据。每个返回值的列都不同,因此没有强类型名称。我正在努力订购这些,因为数字列是作为字符串排序的,例如。 1 - > 10 - > 11 - > 15 - > 2 - > 25 - > 3,而不是价值。我已经确认正确设置了列的ValueType。我试过这个:

var sortedRows = from row in _rowArray
                             orderby columnIndex
                             select row;
                _rowArray = sortedRows.ToArray();

其中_rowArray是DataRows的数组,并且是DataGridView的DataSource,而columnIndex是一个整数变量,指定用户想要排序的_rowArray中的哪一列。在调试时,我可以看到sortedRows按照相同的顺序与_rowArray保持相同的顺序,紧跟在LINQ语句之后。我也尝试过:

_rowArray = _rowArray.OrderBy(r => r[columnIndex]).ToArray();

并尝试创建DataView(视图)并完成此操作:

view.Sort = m_data.Tables[0].Columns[columnIndex].ColumnName + " ASC";        

......但这没效果。有人提供任何见解吗?

2 个答案:

答案 0 :(得分:1)

在订购之前,您是否尝试将字符串解析为int?

_rowArray = _rowArray.OrderBy(r => int.Parse(r[columnIndex])).ToArray();

答案 1 :(得分:0)

感谢lcfseth和线程here的帮助,我解决了这个问题,虽然这很难看。

我使用了其他线程中的Parse方法:

public static T Parse<T>(object value)
{
    try { return (T)System.ComponentModel.TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(value.ToString()); }
    catch { return default(T); }
}

然后我自己的方法变成了:

private void SortRows(int columnIndex, ListSortDirection direction)
        {
            if ((_rowArray == null) || (!_rowArray.Any()))
                return;

            dataGridView1.EndEdit();

            RemoveRowDefinitions();

            if (direction == ListSortDirection.Ascending)
            {
                var dataType = dataGridView1.Columns[columnIndex].ValueType;
                try
                {
                    switch (dataType.Name.ToLower())
                    {
                        case "double":
                            _rowArray = _rowArray.OrderBy(r => Parse<double?>(r[columnIndex].ToString())).ToArray();
                            break;
                        case "int32":
                            _rowArray = _rowArray.OrderBy(r => Parse<int?>(r[columnIndex].ToString())).ToArray();
                            break;
                        case "datetime":
                            _rowArray = _rowArray.OrderBy(r => Parse<DateTime?>(r[columnIndex].ToString())).ToArray();
                            break;
                        default:
                            _rowArray = _rowArray.OrderBy(r => r[columnIndex].ToString()).ToArray();
                            break;
                    }
                }
                catch(Exception ex)
                {
                    throw new Exception("Cannot sort on the data type '" + dataType.Name + "'", ex);
                }              
            }

            if (direction == ListSortDirection.Descending)
            {
                var dataType = dataGridView1.Columns[columnIndex].ValueType;
                try
                {
                    switch (dataType.Name.ToLower())
                    {
                        case "double":
                            _rowArray = _rowArray.OrderByDescending(r => Parse<double?>(r[columnIndex].ToString())).ToArray();
                            break;
                        case "int32":
                            _rowArray = _rowArray.OrderByDescending(r => Parse<int?>(r[columnIndex].ToString())).ToArray();
                            break;
                        case "datetime":
                            _rowArray = _rowArray.OrderByDescending(r => Parse<DateTime?>(r[columnIndex].ToString())).ToArray();
                            break;
                        default:
                            _rowArray = _rowArray.OrderByDescending(r => r[columnIndex].ToString()).ToArray();
                            break;
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("Cannot sort on the data type '" + dataType.Name + "'", ex);
                }                    
            }

            dataGridView1.Refresh();
            ApplyRowDefinitions();
            GC.Collect();
        }