Search datagridview on user keypress

前端 未结 5 1341
别跟我提以往
别跟我提以往 2021-02-09 05:09

I\'m trying to select the first row where the cell value starts with the same keychar the user pressed. That\'s the part that is giving me trouble.

Here\'s how I\'m hand

5条回答
  •  忘了有多久
    2021-02-09 05:34

    if (Char.IsLetterOrDigit(e.KeyChar))
    {
        foreach (DataGridViewRow dgvRow in myDgv.Rows)
        {
            if (dgvRow.Cells["ColumnName"].FormattedValue
                .ToString().StartsWith(e.KeyChar.ToString(), true, CultureInfo.InvariantCulture))
            {
                dgvRow.Selected = true;
                break;
            }
        }
    }
    

    If the DGV is set up to allow Multi-Select then you'd obviously want to deselect any existing selection.

提交回复
热议问题