How do I make the DataGridView show the selected row?

后端 未结 9 1246
傲寒
傲寒 2020-12-07 18:17

I need to force the DataGridView to show the selected row.

In short, I have a textbox that changes the DGV select

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 19:08

    // This works, it's case sensitive and finds the first occurrence of search

        private bool FindInGrid(string search)
        {
            bool results = false;
    
            foreach (DataGridViewRow row in dgvData.Rows)
            {
                if (row.DataBoundItem != null)
                {
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        if (cell.Value.ToString().Contains(search))
                        {
                            dgvData.CurrentCell = cell;
                            dgvData.FirstDisplayedScrollingRowIndex = cell.RowIndex;
                            results = true;
                            break;
                        }
    
                        if (results == true)
                            break;
                    }
                    if (results == true)
                        break;
                }
            }
    
            return results;
        }
    

提交回复
热议问题