这项任务似乎很容易。
我想允许XtraGrid按图像列排序(图像代表一个状态)
根据手册,您只需要将列排序模式设置为Custom,并在CustomColumnSort Eventhandler中添加一些代码。
这里没有用,事件处理程序从未被调用过。
我明确地尝试了以下内容:
gridViewLeftGrid.Columns["ImageColumnName"].OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.True;
gridView.Columns["ImageColumnName"].SortMode = DevExpress.XtraGrid.ColumnSortMode.Custom;
gridView.CustomColumnSort += gridView_CustomColumnSort;
gridView.CustomColumnGroup += grid_CustomColumnGroup;
void gridView_CustomColumnSort(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e)
{
throw new NotImplementedException();
}
但仍然 - 从未到达gridView_CustomColumnSort中的断点。
我看到的唯一影响是,现在每次加载或更新网格时,DevExpress.Data.Storage.DataStorageObjectComparer.CompareRecordsCode
都会抛出错误:
“至少有一个Object应该实现ICompare”
有人可以帮忙失踪吗?
答案 0 :(得分:0)
您的数据源应按顺序实施 IXtraSortable 界面 实现所需的排序
答案 1 :(得分:-1)
我建议您浏览ColumnView.CustomColumnSort Event文档并查看以下内容:
自定义比较的结果应设置为CustomColumnSortEventArgs.Result参数,如下所示:
注意:强>
如果当前比较操作为true
,则CustomColumnSortEventArgs.Handled参数应设置为handled
。您可以将此参数设置为false,以在事件处理程序完成后调用默认比较机制。在这种情况下,将忽略自定义比较操作的结果。
检查示例:
protected void grid_CustomColumnSort
(object sender, DevExpress.Web.ASPxGridView.CustomColumnSortEventArgs e) {
if (e.Column.FieldName == "Country") {
e.Handled = true;
string s1 = e.Value1.ToString(), s2 = e.Value2.ToString();
if (s1.Length > s2.Length)
e.Result = 1;
else
if (s1.Length == s2.Length)
e.Result = Comparer.Default.Compare(s1, s2);
else
e.Result = -1;
}
}
你也可以在这样的事件上实现比较代码:
if (e.Column.FieldName == "Importance")
{
int n1 = (int)gridControl1.GetCellValue(e.ListSourceRowIndex1, "Rank");
int n2 = (int)gridControl1.GetCellValue(e.ListSourceRowIndex2, "Rank");
e.Result = Comparer<int>.Default.Compare(n1, n2); /// comparator here, that set the e.result
e.Handled = true;
}
参考文献:
How to: Implement Custom Sorting
DXGrid CustomColumnSort not sorting correctly
CustomColumnSort not working
ASPxGridView - How to sort groups - 它与asp.net网格有关,但可能会有所帮助
按照此知识库文章How to implement custom sorting,检查引发自定义排序事件的代码段。希望这有帮助:
public partial class GridCustomSortTest : Form
{
public GridCustomSortTest()
{
InitializeComponent();
}
private void GridCustomSortTest_Load(object sender, EventArgs e)
{
string[] months = new string[] { "January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December" };
grid.DataSource = months;
grid.RefreshDataSource();
gridView1.Columns[0].SortMode = DevExpress.XtraGrid.ColumnSortMode.Custom;
gridView1.CustomColumnSort += new DevExpress.XtraGrid.Views.Base.CustomColumnSortEventHandler(gridView1_CustomColumnSort);
}
void gridView1_CustomColumnSort(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e)
{
e.Result = Comparer<int>.Default.Compare(e.ListSourceRowIndex1,
e.ListSourceRowIndex2);
e.Handled = true;
}
}