Setting Focus On DataGrid

Aug 29, 2008 at 4:08 PM
I want to set focus on the first cell of the DataGrid when the window loads. I'm trying this...

personnelDataGrid.SelectedIndex = 0;

The selected index statement is working nicely and placing a highlight on the first row of the grid but I can't figure out why the user can't immediately arrow down the list using the keyboard.  The user has to either press the tab key or use the mouse to get focus on the top row (or any cell of the top row). I guess it's because the grid has focus but the first cell does not. How do I get the grid to focus on the first cell of the top row when the window loads?


Aug 29, 2008 at 8:33 PM
Try something like

DataGridCell cell = GetCell(0,0);

when the window is loaded.  The implementation for this is on this thread,
Aug 29, 2008 at 9:03 PM
When I type DataGridCell it is not in scope. Do I need a using statement at the top of my C#?
Aug 29, 2008 at 9:59 PM
DataGridCell has the same scope as DataGrid.  I'm not sure why you cannot find it.  The using statement should be:





Aug 29, 2008 at 11:00 PM

Adding that reference helped, thanks.

When I try your example from the other thread I get an error from this line
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

The error I get is...



"The non-generic method 'System.Windows.FrameworkElement.GetVisualChild(int)' cannot be used with type arguments"


Aug 29, 2008 at 11:42 PM
GetVisualChild<T>() is actually a method that you are suppose to implement similar to this:



static T GetVisualChild<T>(Visual parent) where T : Visual
    T child =
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v
as T;
        if (child == null)
            child = GetVisualChild<T>(v);
        if (child != null)
    return child;


Aug 31, 2008 at 8:47 PM

Ok thanks. This compiles now but when I step through the GetRow module on this line ...


... I get the following error...

"Operation is not valid while ItemCollection has no inner collection. ItemCollection is uninitialized or binding on ItemsControl.ItemSource supplied null for collection."

Sep 1, 2008 at 6:38 PM
Have you set the personnelDataGrid.ItemsSource already before calling this code?
Oct 24, 2008 at 2:07 PM

I do the following, BEUserGroup is my business object, the grid is already containing some rows, and I want to focus on a cell of a freshly added new item/row, which is added as first row by thre following code:

      Dim oItem As New BE.BEUserGroup
      oItem.UserGrpName = "<Name>"
      oItem.Desc = "<Desc>"
      Call mltData.Add(oItem)

Now I want to focus on a cell in this new row

      Dim oCell As DataGridCell = Helpers.GetCell(dgData, 0, 1)

This dows not work, because GetCell, the presenter is nothing (rowcontainer has no visual children)
         Dim presenter As DataGridCellsPresenter = GetVisualChild(Of DataGridCellsPresenter)(rowContainer)

Is there a workaround for this?

Oct 30, 2008 at 2:09 PM
Have you tried a DataGrid.UpdateLayout() and DataGrid.ScrollIntoView()?
Oct 30, 2008 at 3:37 PM

DataGrid.UpdateLayout allows the focus move, thanks for that.

But is there a way to set the focused cell into edit mode?
After the move focus I can start entering data, but the cursor is not blinking, so the user has no visual clue that he can start entering data.


Oct 31, 2008 at 3:19 PM
After the focus, try calling DataGrid.BeginEdit().
Oct 31, 2008 at 3:32 PM


Thanks for the link.

I already found this, but it really does not help me

Can you hint me in some documentation for
- ValidationStep
- RawProposed or ConvertedProposed
So that I have an alternative? I really can not find anything on this!


Oct 31, 2008 at 5:11 PM
Sorry, I'm a little confused about your question.  You were asking previously, "But is there a way to set the focused cell into edit mode?".  Setting BeginEdit will do that.  What exactly does the ValidationStep have to do with beginning the edit?
Oct 31, 2008 at 5:18 PM


I answered to the wrong mail, I will send the response again to you regarding V1 validation

Regarding SetFocus: BeginEdit does the job! Thank for your support

Best regards