【问题标题】:c# window form DataTable with Image column Sorting时间:2019-01-01 标签:c#windowformDataTablewithImage column Sorting
【发布时间】:2012-04-10 09:35:46
【问题描述】:

我有 DataGridView,我使用 DataTables 设置了 datagridview 的 DataSource。

DataTable dt = new DataTable();

        dt.Columns.Add("Image",typeof(Bitmap));
        dt.Columns.Add("Col2", typeof(string));
        dt.Columns.Add("Col3", typeof(string));
        dt.Columns.Add("Col4", typeof(string));
        dt.Columns.Add("Col5", typeof(string));

        int currentrow = 0;
        foreach (Dev d in Devs)
        {
            dt.Rows.Add(dt.NewRow());
            Bitmap bmp = Test(d);
            dt.Rows[currentrow][0] = bmp;
            dt.Rows[currentrow][1] = d .ID;
            dt.Rows[currentrow][2] = d .Name;
            dt.Rows[currentrow][3] = d .Country;
            dt.Rows[currentrow][4] = d .State;
            currentrow++; 
        }
       datagridview.DataSource = dt;

当我的列类型为字符串时,此代码排序,但我也想根据图像进行排序。我想单击图像列,它应该根据图像进行排序。只有三种类型的图像,所以我希望相同的图像应该放在一起以便于显示。 我搜索但找不到任何解决方案。 有什么可以引导我走向正确的方向吗?

当我尝试这样的事情时出错

 datagridview.Sort(dgvFusePTW.Columns[0], ListSortDirection.Ascending);

错误:数据绑定的 DataGridView 控件只能在数据绑定的列上排序。

更新: 我又增加了一栏。它是隐藏的,当使用单击图像列(第一个)时,它会触发 ColumnHeaderMouseClick 事件。在此处添加逻辑以对隐藏列进行排序。 这只是解决我点击的问题。

谢谢你,

L.E.

【问题讨论】:

    标签: c# sorting datagridview


    【解决方案1】:

    如果你想这样做,你需要使用DataView。 (您需要使用 DataSetExtensions 来利用 LINQ。)

    // the Bitmap class has the RawFormat property that tells whether
    // it's JPG, PNG, BMP, etc etc
    DataView dv = dt.AsEnumerable()
        .OrderBy(c => c.Field<Bitmap>("Image").GetImageOrder()) // sort by image type
        .ThenBy(d => d.Field<string>("Col2")) // then sort by ID...
        .AsDataView();
    
    // take the dataview and bind...
    datagridview.DataSource = dv;
    

    还需要定义如下静态扩展方法:

    public static class ImageHelper
    {
        private static ImageFormat[] supportedFormats = new ImageFormat[]
        {
            ImageFormat.Bmp,
            ImageFormat.Gif,
            ImageFormat.Jpeg,
            ImageFormat.Png,
            ImageFormat.Tiff,
            ImageFormat.Wmf,
            ImageFormat.Emf,
            ImageFormat.Exif
        };
    
        public static int GetImageOrder(this Image target)
        {
            for (int i = 0; i < supportedFormats.Length; i++)
            {
                if (target.RawFormat.Equals(supportedFormats[i]))
                {
                    return i;
                }
            }
    
            // the image format is not within our supported formats array:
            // just order it to the very end
            return 9999;
        }
    }
    

    请注意,supportedFormats 数组有一个我刚刚想到的任意排序顺序 - 您可以按照您想要的任何方式重新排序数组,并且图像应该按照您的意愿重新排序。

    【讨论】:

    • 仅基于图像。不是图像类型。所以只有一种类型的图像。 .ICO 。更新的问题。
    • 我尝试了您的解决方案,但它显示错误,例如“System.ArgumentException:至少一个对象必须实现 IComparable。”
    猜你喜欢
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多