如何对 datagridview 列进行排序出现错误:参数 NUll 异常未处理
我通过以下代码使用以下 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 异常未处理..
Value cannot be null.
Parameter name: dataGridViewColumn
有人能帮忙解决这个问题吗....
I am binding the data grid view by using the following linq to entity framework query by using the following code..
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;
}
I have got the columns product id , product image ,product name ,product desription,product price..
i have made some of the columns are not visible for the purpose of client ..
now i want to sort the columns by clicking on the column header ....
Note: here the product.image is stored as byte of arrays in database ....
i dont know how to compare the bytes and sorting like that....
would any one pls help on this one......
many thanks...
MODIFIED CODE:
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;
}
got An error: Argument NUll Exception Was Unhandled ..
Value cannot be null.
Parameter name: dataGridViewColumn
would any one help on this....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经尝试了以下代码并且它有效,我没有图像,所以我使用了空列。代码有点长,因为我必须实现
BindingList
来实现排序。您可以在 此答案和此处。您可以找到有关AutoPoco
此处。您将在
gv_ColumnHeaderMouseClick
函数中得到您的东西。I have tried the following code and it works, I don't have images so I used empty column. The code is bit long because I had to implement
BindingList<T>
to implement sorting. You can read more about the implementation ofBindingList<T>
in this answer and here. You can find more aboutAutoPoco
here.You will get your thing in
gv_ColumnHeaderMouseClick
function.