Hello,
How do i select or set the focus of a particular cell in a datagridview? Actually, this problem is simple but it is hard for me to solve it coz i'm only a beginner in c#. So here is my sample list of students as shown below:

Ann
Antoinette
Bernadette
Brenda
Carole
Dean
Millicent
Ralph
Suzie
Victoria

During runtime the default cursor position will be at the top which is "Ann", So if i key in a letter or name in a textbox for example "s", automatically the cursor @ the datagrid will set to suzie so the output will be look like this:

Ann
Antoinette
Bernadette
Brenda
Carole
Dean
Millicent
Ralph
Suzie
Victoria

So the position of the cursor was changed and "Suzie" was selected, and if i key in another letter at the textbox automatically the cursor position will change and so on. I think it is still part of sorting in sql and it is very useful in windows environment. I need help for this problem.

Thank you very very much!

Har

Dani AI

Generated

Note: 's fallback to a ListBox makes sense for a simple name list because ListBox provides built-in "type-to-select" behavior. When a DataGridView is required (multiple columns, binding, editability), a compact, reliable pattern is to find the matching row from the TextBox input, pick a visible cell in that row, set the grid's CurrentCell so the cell receives focus and the view scrolls to it, then clear previous selection or mark the row selected. 's suggestion to programmatically select a cell is correct in principle; the snippet below shows a practical TextBox.TextChanged handler that handles hidden columns and keeps the grid visible and focused. (learn.microsoft.com)

private void txtSearch_TextChanged(object sender, EventArgs e)
{
    string q = txtSearch.Text.Trim();
    if (string.IsNullOrEmpty(q)) return;

    for (int r = 0; r < dataGridView.Rows.Count; r++)
    {
        var value = dataGridView.Rows[r].Cells[searchColumnIndex].Value?.ToString();
        if (!string.IsNullOrEmpty(value) && value.StartsWith(q, StringComparison.OrdinalIgnoreCase))
        {
            int visibleCol = -1;
            for (int c = 0; c < dataGridView.Columns.Count; c++)
                if (dataGridView.Columns[c].Visible) { visibleCol = c; break; }

            if (visibleCol < 0) return;
            dataGridView.ClearSelection();
            dataGridView.CurrentCell = dataGridView.Rows[r].Cells[visibleCol];
            dataGridView.Rows[r].Selected = true;
            dataGridView.FirstDisplayedScrollingRowIndex = Math.Max(0, r - 2);
            dataGridView.Focus();
            break;
        }
    }
}

Troubleshooting notes: do not set CurrentCell to a cell in a hidden column or a non-visible row (check Visible first). SelectionMode and MultiSelect change how programmatic selection behaves, and rebinding the DataSource can clear selection and scroll position — saving/restoring the CurrentCell or FirstDisplayedScrollingRowIndex is a common workaround. See the DataGridView CurrentCell and scrolling/index docs for details. (learn.microsoft.com)

Recommended Answers

All 2 Replies

You can use dataGridView.Rows[0].Cells[0].Selected = true; to select specific cells.
You will need to iterate through the cells and check their values against the textbox text to determine whether they should be selected or not.

Hello,

Thanks my problem was solved. Anyway, i found the best solution for that kind of selection, coz instead of using datagrid view, I found an alternative toolbox that fits to that problem through the use of Listbox, coz it has a features of key in selection that automatically scroll or select the cell or list without using any codes.

Thank you very very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.