1

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

我的代码(如下所示)旨在搜索数据网格,如果它找到具有匹配文本的行,则意味着将数据网格选择的索引移动到相应的行。

    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

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

4

2 回答 2

1

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

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

另外,尝试更改SelectedCellsSelectedRows.

于 2013-09-04T15:04:55.467 回答
0

尝试这个

.


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

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

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

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

于 2013-09-04T15:11:08.520 回答