user2311700
user2311700

Reputation: 17

Searching DataGrid for matching values

I Have a data grid which can be queried based on a comboBox choice .

My code (shown below) is meant to search through the datagrid and if it finds a row with a matching piece of text it is meant to move the datagrids selected index to the corresponding row.

    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;

                }
            }

However I am getting the following error

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

Does anyone know how to fix this error ? searching online hasn't givin a solution

Upvotes: 1

Views: 489

Answers (2)

gunr2171
gunr2171

Reputation: 17545

You are trying to change a SelectedCell's row index, which is read-only. If you are trying to change the selected row, you need to set the SelectedIndex for the DataGrid.

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

Also, try changing SelectedCells to SelectedRows.

Upvotes: 1

Abdul Saleem
Abdul Saleem

Reputation: 10622

try this

.


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

or if you want to select a specific cell, then

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

this will select the first column of the desired row..

Upvotes: 0

Related Questions