IngoB
IngoB

Reputation: 2999

Infragistics UltraGrid selecting first row in addition to programmatic selection

I just want to reselect the selected items after reloading data. It works basically but every second time (!?!) the first row gets selected in addition to the rows I select programmatically, I have no idea why. The grid has the standard settings except the one I am setting in code. This is my form code, nothing special I'd say:

    private List<int> _selectedRowsIds;

    public Form1()
    {
        InitializeComponent();

        ultraGrid1.DisplayLayout.Override.CellClickAction = CellClickAction.RowSelect;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SaveSelectedRows();
        Load();
        ReSelectRows();
    }

    private new void Load()
    {
        var data = new List<Address>
        {
            new Address { Id = 1, Street = "rtdst", ZipCode = "11111", City = "dfgsdfg" },
            new Address { Id = 2, Street = "dfgdfg", ZipCode = "22222", City = "sfgdfg" },
            new Address { Id = 3, Street = "dfgdsf", ZipCode = "33333", City = "dfg" },
            new Address { Id = 4, Street = "dfgdfg", ZipCode = "44444", City = "dfgsd" },
            new Address { Id = 5, Street = "dfgdf", ZipCode = "55555", City = "dgsf dfgdfg" },
            new Address { Id = 6, Street = "dfgdfgdf", ZipCode = "66666", City = "dsgds dfg gdf gdfgds" }
        };

        ultraGrid1.DataSource = data;
    }

    public void SaveSelectedRows()
    {
        _selectedRowsIds = new List<int>();

        foreach (UltraGridRow row in ultraGrid1.Selected.Rows)
        {
            _selectedRowsIds.Add(((Address)row.ListObject).Id);
        }
    }

    public void ReSelectRows()
    {
        if (!_selectedRowsIds.Any())
            return;

        ultraGrid1.ActiveRow = null;
        foreach (UltraGridRow row in ultraGrid1.Rows)
        {
            if (_selectedRowsIds.Contains(((Address)row.ListObject).Id))
            {
                row.Selected = true;
            }
        }

        _selectedRowsIds = null;
    }
}

public class Address
{
    public int Id { get; set; }
    public string Street { get; set; }
    public string ZipCode { get; set; }
    public string City { get; set; }
}

}

Upvotes: 0

Views: 171

Answers (1)

IngoB
IngoB

Reputation: 2999

Finally I found it - I had to set one of the selected rows to be the active row.

Selected.Rows[0].Activate();

Upvotes: 0

Related Questions