Infragistics UltraWinGrid选择新添加的行

时间:2016-12-16 02:08:22

标签: c# winforms infragistics ultragrid ultrawingrid

我正在使用Infragistics4.Win.UltraWinGrid.v16.1数据容器。让我们说我刚刚更新了DataSource,并且我希望将该行(它在DataSource更新时显示)显示为活动状态,因为它会滚动到该行并选择它。目前我有以下错误的实现:

private void SelectByID(int searchId)
{
    ISelectionManager selectionManager = grdProjects;

    foreach (UltraGridRow row in grdProjects.Rows)
    {
        if (Convert.ToInt32(row.Cells["StagingID"].Value) == searchId)
        {
            selectionManager.ActivateItem(row);
            row.Activate();
            if (grdProjects.ActiveRowScrollRegion.IsActiveScrollRegion)
                grdProjects.ActiveRowScrollRegion.ScrollRowIntoView(row);
            break;
        }
    }
}

private void _createProjectDialog_FormClosed(object sender, FormClosedEventArgs e)
{
    CreateProjectDialog send = (CreateProjectDialog)sender;
    if (send.SetAsCurrent)
    {
        this.Close();
    }
    else
    {
        //refresh grid and set current proj
        _stagingProjects.Add(send.StagingProject);
        grdProjects.Rows.Refresh(RefreshRow.ReloadData);
        SelectedProject = ApplicationContext.Instance.Project.StagingProject;
        SelectByID(SelectedProject.StagingID); 
    }
}

注意我添加了两行代码:

row.Activate();
if(grdProjects.ActiveRowScrollRegion.IsActiveScrollRegion)

IsActiveScrollRegion条件通过,但执行后仍未在网格中选择该行。

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

如果你想选择特定的行只需使用row.Selected = true;,如果你想激活特定的行,只需使用row.Activated = true;大概你知道这两者之间的区别,我很快就解释了它代码评论。这是一个简单的表单示例。

public partial class Form1 : Form
{
    private DataSet _dataSet = new DataSet();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        InitializeDataSet(_dataSet);
    }

    private void SelectByID(int searchId)
    {
        ISelectionManager selectionManager = this.ultraGrid1;

        foreach (UltraGridRow row in this.ultraGrid1.Rows)
        {
            if (Convert.ToInt32(row.Cells["ID"].Value) == searchId)
            {
                if (ultraGrid1.ActiveRowScrollRegion.IsActiveScrollRegion)
                {
                    ultraGrid1.ActiveRowScrollRegion.ScrollRowIntoView(row);

                    // Activating and selecting are two different things.
                    // Activating the row draws dotted border around it.
                    // Selecting the row highlights it with blue color by default.
                    row.Activated = true;
                    row.Selected = true;
                    break;
                }
            }
        }
    }

    private void ultraButton1_Click(object sender, EventArgs e)
    {
        var dataSourceRows = _dataSet.Tables[0].Rows;
        var dataRow = dataSourceRows.Add(new object[] { dataSourceRows.Count });

        SelectByID(dataSourceRows.Count - 1);
    }

    private void InitializeDataSet(DataSet dataSet)
    {
        var dataTable = dataSet.Tables.Add();

        dataTable.Columns.Add("ID", typeof(int));

        for (int index = 0; index <= 100; index++)
        {
            dataTable.Rows.Add(new object[] { index });
        }

        this.ultraGrid1.DataSource = dataTable;
    }
}

enter image description here

答案 1 :(得分:-2)

grid.Rows[grid.Rows.Count - 1].Selected = true;