Set the selected item of an XAM Datagrid programatically according to its name

13,372

you can select records with the following code (if you want select more than one record)

private void ShowSearchResult(string searchStr)
{
    var recordsToSelect = new List<Record>();
    foreach (Record rec in xamGrid.Records) {
      var yourData = rec is DataRecord ? ((DataRecord)rec).DataItem as YourDataClass : null;
      if (yourData != null && yourData.MatchWithSearchStr(searchStr)) {
        recordsToSelect.Add(rec);
      }
    }
    xamGrid.SelectedItems.Records.Clear();
    // you need linq -> .ToArray()
    xamGrid.SelectedItems.Records.AddRange(recordsToSelect.ToArray(), false, true);
}

or if you only want to activate and select a record then do this one

private void ShowSearchResult(string searchStr)
{
    foreach (Record rec in xamGrid.Records) {
      var yourData = rec is DataRecord ? ((DataRecord)rec).DataItem as YourDataClass : null;
      if (yourData != null && yourData.MatchWithSearchStr(searchStr)) {
        xamGrid.ActiveRecord = rec;
        // don't know if you really need this 
        xamGrid.ActiveRecord.IsSelected = true;
        break;
      }
    }
}

hope this helps

Share:
13,372
User10
Author by

User10

Updated on June 05, 2022

Comments

  • User10
    User10 almost 2 years

    Something along the lines of this.

    private void SearchResult(string nameOfBean)
    {
        foreach (Record VARIABLE in mbeanDataGrid.Records)
        {
            if (VARIABLE.ToString().Contains(nameOfBean))
            {
                ((VARIABLE as DataRecord).DataItem as Record).IsSelected = true;
            }
        }
    }
    

    However i know this syntax is wrong and im looking some advice! Pretty much to select the item (As if you had clicked on it) via code. According to its name.