c#窗口表单DataTable与图像列排序 [英] c# window form DataTable with Image column Sorting

查看:140
本文介绍了c#窗口表单DataTable与图像列排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有DataGridView,并且通过使用DataTable设置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事件。添加了逻辑来排序隐藏的列。
这只是一个点击我的工作。



谢谢你,



LE

解决方案

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

  // Bitmap类有RawFormat属性,它告诉是否
//它是JP​​G,PNG,BMP等等
DataView dv = dt.AsEnumerable()
.OrderBy(c => c.Field< Bitmap>(Image)。GetImageOrder())//按图像排序类型
.ThenBy(d => d.Field< string>(Col2))//然后排序ID ...
.AsDataView();

//获取dataview并绑定...
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;
}
}

//图像格式不在我们支持的格式数组中:
//只是订购到最后
return 9999 ;
}
}

请注意, supportedFormats array有一个任意的排序顺序,我刚刚想到 - 你可以按照你想要的方式对数组进行重新排序,图像应该按照你想要的顺序排列。


I have DataGridView and I set DataSource of datagridview by using DataTables.

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;

This code sort when my column type of string, but i want to sort based on image also. I want to click on image column and it should sort based on images. There are three types of image only, so i want same image should be together for easier display purpose. I searched on but could not find any solution yet. Any thing that can guide me to right direction?

Got Error when i Tried something like this

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

Error : Data-bound DataGridView control can only be sorted on data-bound columns.

UPDATE : I added one more column. It is hidden, when use click on Image column (1st one), it fires ColumnHeaderMouseClick events. Added Logic there to sort hidden column. It is just work around which one clicked for me.

Thanks you,

L.E.

解决方案

You need to use a DataView if you want to do that. (You will need to use DataSetExtensions to leverage the 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;

You also need to define the following static extension method:

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;
    }
}

Note that the supportedFormats array has an arbitrary sort order that I just thought up -- you can reorder the array any way you want and the images should reorder as you wish.

这篇关于c#窗口表单DataTable与图像列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆