gpt4 book ai didi

c# - 根据名称以编程方式设置 EXAM Datagrid 的选定项目

转载 作者:行者123 更新时间:2023-12-02 00:07:39 24 4
gpt4 key购买 nike

与此类似的东西。

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

但是我知道这个语法是错误的,我正在寻找一些建议!几乎可以通过代码选择该项目(就像您单击了它一样)。正如它的名字一样。

最佳答案

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

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

希望这有帮助

关于c# - 根据名称以编程方式设置 EXAM Datagrid 的选定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7470533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com