如何将焦点设置在数据网格/网格视图中的特定行上
本文关键字:网格 视图 焦点 设置 数据网 数据 | 更新日期: 2023-09-27 18:18:45
我有一个数据网格/gridview。我一开始用10行填充网格。每次单击按钮,我一直在向数据网格/gridview添加10行。现在我想在每次填充网格时将焦点设置为最后一行。我可以得到那一行的索引,但我不能把焦点设为那一行
你们中有人知道如何用c#实现它吗?
试试这个
dataGridView1.ClearSelection();
int nRowIndex = dataGridView1.Rows.Count - 1;
dataGridView1.Rows[nRowIndex].Selected = true;
dataGridView1.Rows[nRowIndex].Cells[0].Selected = true;
对于WinForms:
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);
试试这个,它的工作为我
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));
}
}
这篇文章真是天赐佳音。我在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
在我的情况下,我有一个按钮,添加新行到datagridView1
,并激活新添加行的第二个单元格c# Windows窗体应用程序。
int rowIndex = datagridView1.Rows.Count - 1;
datagridView1.CurrentCell = datagridView1.Rows[rowIndex].Cells[1];
datagridView1.BeginEdit(false);
试试这个,我在Extjs 4.2.0中使用下面的脚本片段很好。
//currentIndex is the index of grid row
var rowElement = this.getView().getRecord(currentIndex);
this.getView().focusRow(rowElement);