C# 如何设置datagrid/gridview中特定行的焦点?

C# 如何设置datagrid/gridview中特定行的焦点?,c#,gridview,datagrid,C#,Gridview,Datagrid,我有一个datagrid/gridview。我最初用10行填充网格。每次单击一个按钮,我都会继续向datagrid/gridview添加10行。现在,我想在每次填充网格时将焦点设置为最后一行。我可以得到那一行的索引,但是我不能将焦点设置到那一行 你们中有人知道如何用C#来做吗?试试这个 dataGridView1.ClearSelection(); int nRowIndex = dataGridView1.Rows.Count - 1; dataGridView1.Rows[nRowInde

我有一个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;
试试这个

DataGridViewRow rowToSelect = this.dgvJobList.CurrentRow;

rowToSelect.Selected = true;



rowToSelect.Cells[0].Selected = true;

this.dgvJobList.CurrentCell = rowToSelect.Cells[0];

this.dgvJobList.BeginEdit(true);

对于WinForms DataGridView:

myDataGridView.CurrentCell = myDataGridView.Rows[theRowIndex].Cells[0];
对于WebForms GridView,请使用属性


试试这个,我在ExtJS4.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));
        }
    }
publicstaticvoiditemsetfocus(DataGrid-Dg,int-selfindex)
{
如果(Dg.Items.Count>=1&&selfindex
这篇文章是上帝派来的。我在Visual Studio 2017中使用VB,只需要转到数据网格的底部,以允许用户输入数据。通过研究这些答案,我提出了这个解决方案,并认为其他人可能会发现它很有用

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

朋友,我正在该按钮的单击事件中设置焦点。我找不到gridview的任何属性为“行”和“.Selected”。请告诉我如何获取它们。谢谢。我尝试了您的代码,但它给出的错误为“GridviewRow不包含'Selected'的定义,并且找不到接受System.Web.UI.WebControl.GridviewRow类型的第一个参数的扩展方法'Selected'“.I是否缺少任何指令或程序集引用?@Sukanya有一个。您正在使用WebFormGridView吗?如果是,请在问题中标记。朋友,我使用的是.net 4.0。我正在创建web表单。我只得到2个asp网格,它们是DataGrid和Gridview。对于这两个网格,我没有得到.CurrentCell属性。我这边有什么问题吗?第二点是我已经能够使用“SelectedIndex”将点设置为特定的行索引,但我的问题是我无法将焦点集中到该特定行。。
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