开发者

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

开发者 https://www.devze.com 2023-04-05 16:16 出处:网络
Something along the lines of this. private void SearchResult(s开发者_StackOverflowtring nameOfBean)

Something along the lines of this.

private void SearchResult(s开发者_StackOverflowtring 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号