如何将焦点设置在datagrid / gridview中的特定行?

我有一个datagrid / gridview。 我最初用10行填充网格。 每按一次按钮,我就会继续向datagrid / gridview添加10行。 现在我想在每次填充网格时将焦点设置到最后一行。 我可以获得该行的索引,但我无法将焦点设置为该特定行。

你们中的任何人都知道如何在C#中做到这一点吗?

试试这个

dataGridView1.ClearSelection(); int nRowIndex = dataGridView1.Rows.Count - 1; dataGridView1.Rows[nRowIndex].Selected = true; dataGridView1.Rows[nRowIndex].Cells[0].Selected = true; 

对于WinForms DataGridView:

 myDataGridView.CurrentCell = myDataGridView.Rows[theRowIndex].Cells[0]; 

对于WebForms GridView,请使用SelectedIndex属性

 myGridView.SelectedIndex = theRowIndex; 

试试这个…

 DataGridViewRow rowToSelect = this.dgvJobList.CurrentRow; rowToSelect.Selected = true; rowToSelect.Cells[0].Selected = true; this.dgvJobList.CurrentCell = rowToSelect.Cells[0]; this.dgvJobList.BeginEdit(true); 

试试这个,我在Extjs 4.2.0中使用下面的脚本片段。

 //currentIndex is the index of grid row var rowElement = this.getView().getRecord(currentIndex); this.getView().focusRow(rowElement); 

试试这个,这对我有用

  public static void ItemSetFocus(DataGrid Dg, int SelIndex) { if (Dg.Items.Count >= 1 && SelIndex < Dg.Items.Count) { Dg.ScrollIntoView(Dg.Items.GetItemAt(SelIndex)); Dg.SelectionMode = DataGridSelectionMode.Single; Dg.SelectionUnit = DataGridSelectionUnit.FullRow; Dg.SelectedIndex = SelIndex; DataGridRow row = (DataGridRow)Dg.ItemContainerGenerator.ContainerFromIndex(SelIndex); row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); } } 

这篇post一直是神派。 我在Visual Studio 2017中使用VB,只需要转到DATAGRID的底部以允许用户输入数据。 通过研究这些答案,我提出了这个解决方案,并认为其他人可能会觉得它很有用。

 Private Sub AddBUTTON_Click(sender As Object, e As RoutedEventArgs) Handles AddBUTTON.Click ' Go to bottom to allow user to add item in last row DataGrid.Focus() DataGrid.UnselectAll() DataGrid.SelectedIndex = 0 DataGrid.ScrollIntoView(DataGrid.SelectedItem) Dim endofitems As Integer = DataGrid.Items.Count DataGrid.ScrollIntoView(DataGrid.Items.GetItemAt(endofitems - 1)) End Sub