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

如何关闭弹窗后保留Firefly.Box Grid的选中行与滚动位置

解决方案

1. 绕过ActiveRowIndex只读限制,恢复选中行

ActiveRowIndex只读是因为Firefly.Box的Grid选中行与数据源实体绑定,无法直接通过索引设置。正确做法是**存储行的唯一标识(如主键)**而非行索引,再通过数据源的MoveTo方法定位目标行:

// 替换原有的selectedRowIndex,改为存储主键
private int? _selectedRowPrimaryKey;
private Firefly.Box.UI.GridStartOnRowPosition _scrollPosition;

void btnSc_Button_Statistics_Click(object sender, ButtonClickEventArgs e)
{
    // 获取当前选中行的实体(假设实体类为MainTableEntity,主键为Id)
    var currentRow = grdMainTable.DataSource.Current as MainTableEntity;
    if (currentRow != null)
        _selectedRowPrimaryKey = currentRow.Id;
    
    _scrollPosition = grdMainTable.StartOnRowPosition;
    e.Raise(Command.Expand);
    
    // 模态窗口关闭后立即恢复状态(无需等到Form_Load)
    RestoreTableState();
}

private void RestoreTableState()
{
    if (_scrollPosition != null)
    {
        // 先恢复滚动位置
        grdMainTable.StartOnRowPosition = _scrollPosition;
    }

    if (_selectedRowPrimaryKey.HasValue)
    {
        var dataSource = grdMainTable.DataSource as Firefly.Box.Entity;
        if (dataSource != null)
        {
            // 定位到对应主键的行,自动选中
            dataSource.MoveTo(row => row["Id"] == _selectedRowPrimaryKey.Value);
            // 再次设置滚动位置,避免MoveTo触发自动滚动
            grdMainTable.StartOnRowPosition = _scrollPosition;
        }
    }
}

2. 保持原表格视图的关键调整

问题核心是MoveTo方法默认会将目标行滚动到视图顶部,因此需要先恢复滚动位置,定位行后再次重置滚动位置,这样就能保留原有滚动状态,选中行不会偏移到顶部。

同时避免在Form_Load中恢复状态——关闭弹窗后窗体不一定会重新加载,直接在弹窗关闭后的代码流中执行恢复更可靠。

3. Firefly.Box内置的视图状态持久化方法

Firefly.Box的Grid提供SaveViewState和RestoreViewState方法,可一键保存/恢复包括选中行、滚动位置、排序、过滤在内的所有视图状态,比手动存储更简洁:

private string _gridViewState;

void btnSc_Button_Statistics_Click(object sender, ButtonClickEventArgs e)
{
    // 保存完整视图状态
    _gridViewState = grdMainTable.SaveViewState();
    e.Raise(Command.Expand);
    
    // 窗口关闭后恢复
    if (!string.IsNullOrEmpty(_gridViewState))
    {
        grdMainTable.RestoreViewState(_gridViewState);
    }
}

注意:部分早期Firefly版本可能没有这两个方法,若项目版本不支持,使用第一种手动存储主键的方案即可。


内容的提问来源于stack exchange,提问作者Noor ul ain Ibrahim

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 03:27:31