0

我使用以下 linq 将数据网格视图绑定到使用以下代码的实体框架查询。

        private void EquipmentFinder_Load(object sender, EventArgs e)
    {
        SetupCategories();
        productgridview.RowTemplate.Height = 130;
        var products = from prods in axe.product1
                       select new
                       {
                           productid = prods.product_Id,   //0                            
                           productnam =  prods.product_Name, //1
                           productimage = prods.product_Image, //2
                           productprice = prods.product_Price,//3
                           productdescr = prods.product_Description, //4

                       };

        productbindingsource.DataSource = products;
        productgridview.DataSource = productbindingsource;          
        productgridview.Columns[0].Visible = false;
        productgridview.Columns[4].Visible = false;
    }  

我有列产品ID,产品图片,产品名称,产品描述,产品价格..

为了客户的目的,我已经制作了一些列不可见..

现在我想通过单击列标题对列进行排序....

注意:这里 product.image 存储为数据库中的数组字节 ....

我不知道如何比较字节和排序......

任何人都可以帮助解决这个问题......

非常感谢...

修改后的代码:

          private void productgridview_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
       DataGridViewColumn newcolumn =  productgridview.Columns.GetColumnCount(DataGridViewElementStates.Selected) == 1 ? productgridview.SelectedColumns[0] : null;
       DataGridViewColumn oldColumn = productgridview.SortedColumn;
       ListSortDirection direction;

       if (oldColumn != null)
       {
           // Sort the same column again, reversing the SortOrder.
           if (oldColumn == newcolumn &&
               productgridview.SortOrder == SortOrder.Ascending)
           {
               direction = ListSortDirection.Descending;
           }
           else
           {
               // Sort a new column and remove the old SortGlyph.
               direction = ListSortDirection.Ascending;
               oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
           }
       }
       else
       {
           direction = ListSortDirection.Ascending;
       }


           productgridview.Sort(newcolumn, direction);
           newcolumn.HeaderCell.SortGlyphDirection =
               direction == ListSortDirection.Ascending ?
               SortOrder.Ascending : SortOrder.Descending;



    }

得到一个错误:参数 NUll Exception Was Unhandled ..

           Value cannot be null.
           Parameter name: dataGridViewColumn

任何人都可以帮助解决这个问题......

4

1 回答 1

1

我已经尝试了以下代码并且它有效,我没有图像所以我使用了空列。代码有点长,因为我必须实现BindingList<T>才能实现排序。BindingList<T>您可以在此答案此处阅读有关实施的更多信息。您可以在此处找到更多信息。AutoPoco

using AutoPoco.Engine;
using AutoPoco;
using AutoPoco.DataSources;

namespace GridViewSorting
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();
        }

        private void TestForm_Load(object sender, EventArgs e)
        {
            LoadGridData();
        }

        private void gv_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var newcolumn = gv.Columns[e.ColumnIndex];
            var showColumn = newcolumn;

            ListSortDirection direction;
            var sortedColumn = gv.SortedColumn;

            var sd = sortedColumn==null? SortOrder.None:sortedColumn.HeaderCell.SortGlyphDirection;

            if (sortedColumn == newcolumn && sd == gv.SortOrder)
                return;

            if (sd == SortOrder.Descending || sd == SortOrder.None)
            {
                sd = SortOrder.Ascending;
                direction = ListSortDirection.Ascending;
            }
            else
            {
                sd = SortOrder.Descending;
                direction = ListSortDirection.Descending;
            }

            //now the fun begins, suppose this is image column and you want to 
            //sort based on product name when product image column header
            //is clicked.

            if (newcolumn.HeaderText == "ProductImage")//check if image column
            {
                newcolumn = gv.Columns["ProductName"];//sort on product names
            }

            gv.Sort(newcolumn, direction);
            newcolumn.HeaderCell.SortGlyphDirection = SortOrder.None;
            showColumn.HeaderCell.SortGlyphDirection = sd;//change sort indicator on clicked column
        }

        private void LoadGridData()
        {
            IGenerationSessionFactory factory = AutoPocoContainer.Configure(x =>
            {
                x.Conventions(c => { c.UseDefaultConventions(); });
                x.AddFromAssemblyContainingType<SimpleProduct>();

                x.Include<SimpleProduct>()
                    .Setup(c => c.ProductName).Use<FirstNameSource>()
                    .Setup(c => c.Id).Use<IntegerIdSource>()
                    .Setup(c => c.ProductDescription).Use<RandomStringSource>(5, 20);
            });

            var session = factory.CreateSession();
            var r = new Random(234234);
            var rn = r.Next(5, 100);
            IList<SimpleProduct> products = session.List<SimpleProduct>(25)
                .Impose(x => x.Price, r.Next() * rn)
                .Get();
            var bl = new ProductList();

            foreach (var i in products)
            {
                bl.Add(i);
            }

            gv.DataSource = bl;
        }
    }

    public class ProductList : SortableProductList<SimpleProduct>
    {
        protected override Comparison<SimpleProduct> GetComparer(PropertyDescriptor prop)
        {
            Comparison<SimpleProduct> comparer;
            switch (prop.Name)
            {
                case "Id":
                    comparer = new Comparison<SimpleProduct>(delegate(SimpleProduct x, SimpleProduct y)
                    {
                        if (x != null)
                            if (y != null)
                                return (x.Id.CompareTo(y.Id));
                            else
                                return 1;
                        else if (y != null)
                            return -1;
                        else
                            return 0;
                    });
                    break;
                case "ProductName":
                    comparer = new Comparison<SimpleProduct>(delegate(SimpleProduct x, SimpleProduct y)
                    {
                        if (x != null)
                            if (y != null)
                                return (x.ProductName.CompareTo(y.ProductName));
                            else
                                return 1;
                        else if (y != null)
                            return -1;
                        else
                            return 0;
                    });
                    break;
                case "ProductDescription":
                    comparer = new Comparison<SimpleProduct>(delegate(SimpleProduct x, SimpleProduct y)
                    {
                        if (x != null)
                            if (y != null)
                                return (x.ProductDescription.CompareTo(y.ProductDescription));
                            else
                                return 1;
                        else if (y != null)
                            return -1;
                        else
                            return 0;
                    });
                    break;
                case "Price":
                    comparer = new Comparison<SimpleProduct>(delegate(SimpleProduct x, SimpleProduct y)
                    {
                        if (x != null)
                            if (y != null)
                                return (x.Price.CompareTo(y.Price));
                            else
                                return 1;
                        else if (y != null)
                            return -1;
                        else
                            return 0;
                    });
                    break;
                default:
                    comparer = new Comparison<SimpleProduct>((x, y) =>
                    {
                        if (x != null && y != null)
                            return x.GetHashCode().CompareTo(y.GetHashCode());
                        return 0;
                    });
                    break;
            }
            return comparer;
        }
    }

    public abstract class SortableProductList<T> : BindingList<T>
    {
        protected override bool SupportsSortingCore{get{return true;}}

        protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
        {
            if (prop.PropertyType.GetInterface("IComparable") == null)return;
            var itemsList = (List<T>)this.Items;

            Comparison<T> comparer = GetComparer(prop);
            itemsList.Sort(comparer);
            if (direction == ListSortDirection.Descending) itemsList.Reverse();
        }

        protected abstract Comparison<T> GetComparer(PropertyDescriptor prop);
    }

    public class SimpleProduct
    {
        public int Id { get; set; }
        public string ProductName { get; set; }
        public decimal Price { get; set; }
        public string ProductImage { get; set; }
        public string ProductDescription { get; set; }
    }
}

你会让你的东西gv_ColumnHeaderMouseClick发挥作用。

于 2011-08-23T12:11:59.480 回答