How to set focus on a particular row in a datagrid/gridview?

前端 未结 6 1523
清酒与你
清酒与你 2021-01-04 18:07

I have a datagrid/gridview. I\'m populating the grid with 10 rows initially. On a button click every time,I\'m keeping on adding 10 rows to the datagrid/gridview. Now I want

相关标签:
6条回答
  • 2021-01-04 18:45

    Try this...

    DataGridViewRow rowToSelect = this.dgvJobList.CurrentRow;
    
    rowToSelect.Selected = true;
    
    
    
    rowToSelect.Cells[0].Selected = true;
    
    this.dgvJobList.CurrentCell = rowToSelect.Cells[0];
    
    this.dgvJobList.BeginEdit(true);
    
    0 讨论(0)
  • 2021-01-04 18:51

    For WinForms DataGridView:

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

    For WebForms GridView, use the SelectedIndex property

    myGridView.SelectedIndex = theRowIndex;
    
    0 讨论(0)
  • 2021-01-04 18:53

    Try this

    dataGridView1.ClearSelection();
    int nRowIndex = dataGridView1.Rows.Count - 1;
    
    dataGridView1.Rows[nRowIndex].Selected = true;
    dataGridView1.Rows[nRowIndex].Cells[0].Selected = true;
    
    0 讨论(0)
  • 2021-01-04 18:53

    Try this, it's work for me

       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));
            }
        }
    
    0 讨论(0)
  • 2021-01-04 18:54

    Try this, I'm works well with below script snippet in Extjs 4.2.0.

    //currentIndex is the index of grid row
    var rowElement = this.getView().getRecord(currentIndex);
    this.getView().focusRow(rowElement);
    
    0 讨论(0)
  • 2021-01-04 19:02

    This post has been a god send. I am using VB in Visual Studio 2017 and just needed to go to the bottom of a DATAGRID to allow user to input data. By studying these answers I came up with this solution and thought others might find it useful.

    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
    
    0 讨论(0)
提交回复
热议问题