Подтвердить что ты не робот

DataGridView - Фокусировка определенной ячейки

Как установить фокус на любую указанную ячейку в DataGridView? Я ожидал простой способ, например Focus (rowindex, columnindex), но это не так просто.

4b9b3361

Ответ 1

Установите текущую ячейку следующим образом:

DataGridView1.CurrentCell = DataGridView1.Rows[rowindex].Cells[columnindex]

или

DataGridView1.CurrentCell = DataGridView1.Item("ColumnName", 5)

и вы можете напрямую сфокусироваться на "Редактирование":

dataGridView1.BeginEdit(true)

Ответ 2

вы можете установить Focus на конкретный Cell, установив для свойства Selected значение true

dataGridView1.Rows[rowindex].Cells[columnindex].Selected = true;

чтобы избежать множественного выбора, просто установите

dataGridView1.MultiSelect = false;

Ответ 3

проблема с datagridview заключается в том, что она автоматически выбирает первую строку, поэтому вы хотите очистить выбор

grvPackingList.ClearSelection();
dataGridView1.Rows[rowindex].Cells[columnindex].Selected = true;  

другой мудрый он не будет работать

Ответ 4

У меня была аналогичная проблема. Я спрятал несколько столбцов, а затем попытался выбрать первую строку. Это действительно не работает:

datagridview1.Rows[0].Selected = true;

Поэтому я попытался выбрать cell[0,0], но он также не работал, потому что эта ячейка не отображалась. Теперь мое окончательное решение работает очень хорошо:

datagridview1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;    
datagridview1.CurrentCell = datagridview1.FirstDisplayedCell;

Итак, это выбирает полную первую строку.

Ответ 5

public void M(){ 
  dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
  dataGridView1.CurrentCell.Selected = true; 
  dataGridView1.BeginEdit(true);
}

Ответ 6

DataGridView1.CurrentCell = DataGridView1.Item( "ColumnName", 5)

Ответ 7

в событии form_load (отправитель объекта, EventArgs e) попробуйте это

dataGridView1.CurrentCell = dataGridView1.Rows [dataGridView1.Rows.Count1].Cells [0];

этот код делает фокус на последней строке и первой ячейке

Ответ 8

 private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        int row = e.RowIndex;
        int col = e.ColumnIndex;
        if (row < 0 || col != 3)
            return;
        if (e.FormattedValue.ToString().Equals(String.Empty))
        {
        }

        else
        {
            double quantity = 0;
            try
            {
                quantity = Convert.ToDouble(e.FormattedValue.ToString());
                if (quantity == 0)
                {
                    MessageBox.Show("The quantity can not be Zero", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    e.Cancel = true;
                    return;
                }
            }
            catch
            {
                MessageBox.Show("The quantity should be decimal value.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
                return;
            }
        }
    }

Ответ 9

Просто простая вставка и передача Gridcolor() где угодно.

Private Sub Gridcolor()
    With Me.GridListAll
        .SelectionMode = DataGridViewSelectionMode.FullRowSelect
        .MultiSelect = False
        '.DefaultCellStyle.SelectionBackColor = Color.MediumOrchid
    End With
End Sub