Reputation: 1061
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 to set the focus to the last row every time the grid is populated. I can get the index of that row,but I can't set focus to that particular row.
Do anyone of you have any idea how to do it in C#?
Upvotes: 13
Views: 88171
Reputation: 57
In my case, I had a button which add new row to datagridView1
and activate newly added row's second cell C# Windows Form Application.
int rowIndex = datagridView1.Rows.Count - 1;
datagridView1.CurrentCell = datagridView1.Rows[rowIndex].Cells[1];
datagridView1.BeginEdit(false);
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 107
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));
}
}
Upvotes: 0
Reputation: 51
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);
Upvotes: -1
Reputation: 620
For WinForms DataGridView:
myDataGridView.CurrentCell = myDataGridView.Rows[theRowIndex].Cells[0];
For WebForms GridView, use the SelectedIndex property
myGridView.SelectedIndex = theRowIndex;
Upvotes: 10
Reputation: 22994
Try this
dataGridView1.ClearSelection();
int nRowIndex = dataGridView1.Rows.Count - 1;
dataGridView1.Rows[nRowIndex].Selected = true;
dataGridView1.Rows[nRowIndex].Cells[0].Selected = true;
Upvotes: 17
Reputation: 1402
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);
Upvotes: 2