【问题标题】:Searching DataGrid for matching values在 DataGrid 中搜索匹配值
【发布时间】:2013-09-04 15:01:12
【问题描述】:

我有一个可以根据组合框选项查询的数据网格。

我的代码(如下所示)用于搜索数据网格,如果找到具有匹配文本的行,则将数据网格选定的索引移动到相应的行。

    for (int i = 0; i <= DashBoard_DataGrid.Columns.Count - 1; i++)
            {
                if  (DashBoard_DataGrid.Rows[0].ToString().ToLower().Contains(comboBox9.Text.ToString().ToLower()))
                {
                    value = dr.Cells[i].Value.ToString();
                    // return dr.Cells[i].RowIndex;
                    DashBoard_DataGrid.SelectedCells[i].RowIndex =  dr.Cells[i].RowIndex;

                }
            }

但是我收到以下错误

           Error    7   Property or indexer 'System.Windows.Forms.DataGridViewCell.RowIndex' cannot be assigned to -- it is read only

有谁知道如何解决这个错误?在线搜索并没有给出解决方案

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    您正在尝试更改SelectedCell 的行索引,该索引是只读的。如果您尝试更改所选行,则需要为 DataGrid 设置SelectedIndex

    DashBoard_DataGrid.SelectedIndex = dr.Cells[i].RowIndex;
    

    另外,请尝试将SelectedCells 更改为SelectedRows

    【讨论】:

    • 我之前尝试过,但我收到错误错误 8 'System.Windows.Forms.DataGridView' 不包含 'SelectedIndex' 的定义并且没有扩展方法 'SelectedIndex' 接受可以找到“System.Windows.Forms.DataGridView”类型的第一个参数(您是否缺少 using 指令或程序集引用?)
    • @user2311700,this 对您有帮助吗?
    • 不,我已经把它放进去,但仍然尝试使用 Sayka 的代码尝试错误,它不会抛出任何错误,但它什么也没做
    【解决方案2】:

    试试这个

    .
    
    
    DashBoard_DataGrid.ClearSelection();
    DashBoard_DataGrid.Rows[3].Selected = true;
    

    或者如果你想选择一个特定的单元格,那么

    DashBoard_DataGrid.ClearSelection();
    DashBoard_DataGrid[0, i].Selected = true;
    

    这将选择所需行的第一列..

    【讨论】:

      猜你喜欢
      • 2011-07-10
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 2014-07-10
      • 2016-05-02
      相关资源
      最近更新 更多