我通过使用以下代码使用以下linq to entity framework查询来绑定数据网格视图。
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异常未处理..
Value cannot be null.
Parameter name: dataGridViewColumn
对此有任何帮助......
答案 0 :(得分:1)
我已经尝试了以下代码并且它有效,我没有图像所以我使用空列。代码有点长,因为我必须实现BindingList<T>
来实现排序。您可以在this answer和here中详细了解BindingList<T>
的实施情况。您可以找到有关AutoPoco
here。
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
函数中得到你的东西。