You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SuperGridControl交换DataItem后数据源未变更的技术问题咨询

问题原因及解决方案

问题根源

你当前的代码仅交换了UI层的GridRow对象位置,但SuperGridControl是基于数据源绑定工作的:GridRows是数据源在UI上的映射视图,直接修改Rows的顺序只会改变显示效果,不会同步到底层的数据源集合,因此数据源不会发生变化。

正确实现方式

必须直接操作绑定的数据源集合,再让UI同步数据源的变化。以下是调整后的代码(假设你的数据源是List<YourDataModel>,请替换YourDataModel为实际的数据实体类型):

行下移代码

// 获取绑定的数据源
var dataSource = sgcData.DataSource as List<YourDataModel>;
if (dataSource == null) return;

int activeRowIndex = sgcData.PrimaryGrid.ActiveRow.Index;
// 最后一行无法下移,直接返回
if (activeRowIndex >= dataSource.Count - 1) return;

// 交换数据源中的元素
var tempItem = dataSource[activeRowIndex];
dataSource[activeRowIndex] = dataSource[activeRowIndex + 1];
dataSource[activeRowIndex + 1] = tempItem;

// 刷新网格,同步UI显示
sgcData.PrimaryGrid.Refresh();
// 将激活行设置到交换后的位置
sgcData.PrimaryGrid.SetActiveRow(sgcData.PrimaryGrid.Rows[activeRowIndex + 1]);

行上移代码

// 获取绑定的数据源
var dataSource = sgcData.DataSource as List<YourDataModel>;
if (dataSource == null) return;

int activeRowIndex = sgcData.PrimaryGrid.ActiveRow.Index;
// 第一行无法上移,直接返回
if (activeRowIndex <= 0) return;

// 交换数据源中的元素
var tempItem = dataSource[activeRowIndex];
dataSource[activeRowIndex] = dataSource[activeRowIndex - 1];
dataSource[activeRowIndex - 1] = tempItem;

// 刷新网格,同步UI显示
sgcData.PrimaryGrid.Refresh();
// 将激活行设置到交换后的位置
sgcData.PrimaryGrid.SetActiveRow(sgcData.PrimaryGrid.Rows[activeRowIndex - 1]);

额外注意事项

  • 如果你的数据源使用ObservableCollection<YourDataModel>(实现了INotifyCollectionChanged接口),无需手动调用Refresh,集合的变化会自动通知网格更新UI。
  • 所有面向数据绑定控件的操作,核心原则都是修改数据源而非直接修改UI元素,UI仅作为数据源的视图呈现。

内容的提问来源于stack exchange,提问作者Csharp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 21:12:30