根据名称以编程方式设置XAM Datagrid的选定项

时间:2011-09-19 11:55:56

标签: c# wpf datagrid infragistics selecteditem

有些事情。

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

但是我知道这种语法是错误的,我正在寻找一些建议!几乎可以通过代码选择项目(就像你点击它一样)。根据它的名字。

1 个答案:

答案 0 :(得分:6)

您可以使用以下代码选择记录(如果您想要选择多条记录)

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);
}

或者如果您只想激活并选择一条记录,请执行此操作

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;
      }
    }
}

希望这会有所帮助