DataGrid select last cell programmatically












0















When the DataGrid below gets the focus for the first time and only the first time (ie, after some other control has had the focus), the last row, 2nd column should be focused and in edit.



enter image description here



I added a handler for the DataGrid.GotFocus, but it's complicated code and not getting the result above.



Anyone got an elegant, bullet proof solution?





I made tiny modifications to the code




  1. the sender should always be the grid I want, so I just used that instead of relying on a name

  2. When the SelectionUnit is FullRow, as my grid was before I changed it to CellOrRowHeader you
    apparently can't call SelectedCells.Clear()


Code below:



private void OnDataGridKeyboardGotFocus(object sender, KeyboardFocusChangedEventArgs e)
{
var dg = sender as DataGrid;
if (_hasHadInitialFocus) return;

var rowIndex = dg.Items.Count - 2;
if (rowIndex >= 0 && dg.Columns.Count - 1 >= 0)
{
var column = dg.Columns[dg.Columns.Count - 1];
var item = dg.Items[rowIndex];
var dataGridCellInfo = new DataGridCellInfo(item, column);

if (dg.SelectionUnit != DataGridSelectionUnit.FullRow) {
dg.SelectedCells.Clear();
dg.SelectedCells.Add(dataGridCellInfo);
}
else {
var row = dg.GetRow(rowIndex);
row.IsSelected = true;
}

dg.CurrentCell = dataGridCellInfo;
dg.BeginEdit();
}

_hasHadInitialFocus = true;
}


New Question



I want to repeat the selection when the focus goes to another control in the window and then back to the grid.
I thought I could turn that _hasHadInitialFocus latch to false in a LostFocus event, but the code below is firing on cell changes.
Do you know how I should be trapping the lost focus event better, and do you agree that is the place to turn the latch off?



    private void DataGridLostFocus(object sender, RoutedEventArgs e) {
_hasHadInitialFocus = false;
}









share|improve this question

























  • You could perhaps look at e.NewFocus, e.OldFocus, e.OriginalSource in DataGridLostFocus and do something clever with those. I'm afraid my source of info on the DataGrid is probably the same as yours. MSDN, StackOverflow and plenty of experimentation.

    – Phil
    Mar 4 '12 at 9:35
















0















When the DataGrid below gets the focus for the first time and only the first time (ie, after some other control has had the focus), the last row, 2nd column should be focused and in edit.



enter image description here



I added a handler for the DataGrid.GotFocus, but it's complicated code and not getting the result above.



Anyone got an elegant, bullet proof solution?





I made tiny modifications to the code




  1. the sender should always be the grid I want, so I just used that instead of relying on a name

  2. When the SelectionUnit is FullRow, as my grid was before I changed it to CellOrRowHeader you
    apparently can't call SelectedCells.Clear()


Code below:



private void OnDataGridKeyboardGotFocus(object sender, KeyboardFocusChangedEventArgs e)
{
var dg = sender as DataGrid;
if (_hasHadInitialFocus) return;

var rowIndex = dg.Items.Count - 2;
if (rowIndex >= 0 && dg.Columns.Count - 1 >= 0)
{
var column = dg.Columns[dg.Columns.Count - 1];
var item = dg.Items[rowIndex];
var dataGridCellInfo = new DataGridCellInfo(item, column);

if (dg.SelectionUnit != DataGridSelectionUnit.FullRow) {
dg.SelectedCells.Clear();
dg.SelectedCells.Add(dataGridCellInfo);
}
else {
var row = dg.GetRow(rowIndex);
row.IsSelected = true;
}

dg.CurrentCell = dataGridCellInfo;
dg.BeginEdit();
}

_hasHadInitialFocus = true;
}


New Question



I want to repeat the selection when the focus goes to another control in the window and then back to the grid.
I thought I could turn that _hasHadInitialFocus latch to false in a LostFocus event, but the code below is firing on cell changes.
Do you know how I should be trapping the lost focus event better, and do you agree that is the place to turn the latch off?



    private void DataGridLostFocus(object sender, RoutedEventArgs e) {
_hasHadInitialFocus = false;
}









share|improve this question

























  • You could perhaps look at e.NewFocus, e.OldFocus, e.OriginalSource in DataGridLostFocus and do something clever with those. I'm afraid my source of info on the DataGrid is probably the same as yours. MSDN, StackOverflow and plenty of experimentation.

    – Phil
    Mar 4 '12 at 9:35














0












0








0








When the DataGrid below gets the focus for the first time and only the first time (ie, after some other control has had the focus), the last row, 2nd column should be focused and in edit.



enter image description here



I added a handler for the DataGrid.GotFocus, but it's complicated code and not getting the result above.



Anyone got an elegant, bullet proof solution?





I made tiny modifications to the code




  1. the sender should always be the grid I want, so I just used that instead of relying on a name

  2. When the SelectionUnit is FullRow, as my grid was before I changed it to CellOrRowHeader you
    apparently can't call SelectedCells.Clear()


Code below:



private void OnDataGridKeyboardGotFocus(object sender, KeyboardFocusChangedEventArgs e)
{
var dg = sender as DataGrid;
if (_hasHadInitialFocus) return;

var rowIndex = dg.Items.Count - 2;
if (rowIndex >= 0 && dg.Columns.Count - 1 >= 0)
{
var column = dg.Columns[dg.Columns.Count - 1];
var item = dg.Items[rowIndex];
var dataGridCellInfo = new DataGridCellInfo(item, column);

if (dg.SelectionUnit != DataGridSelectionUnit.FullRow) {
dg.SelectedCells.Clear();
dg.SelectedCells.Add(dataGridCellInfo);
}
else {
var row = dg.GetRow(rowIndex);
row.IsSelected = true;
}

dg.CurrentCell = dataGridCellInfo;
dg.BeginEdit();
}

_hasHadInitialFocus = true;
}


New Question



I want to repeat the selection when the focus goes to another control in the window and then back to the grid.
I thought I could turn that _hasHadInitialFocus latch to false in a LostFocus event, but the code below is firing on cell changes.
Do you know how I should be trapping the lost focus event better, and do you agree that is the place to turn the latch off?



    private void DataGridLostFocus(object sender, RoutedEventArgs e) {
_hasHadInitialFocus = false;
}









share|improve this question
















When the DataGrid below gets the focus for the first time and only the first time (ie, after some other control has had the focus), the last row, 2nd column should be focused and in edit.



enter image description here



I added a handler for the DataGrid.GotFocus, but it's complicated code and not getting the result above.



Anyone got an elegant, bullet proof solution?





I made tiny modifications to the code




  1. the sender should always be the grid I want, so I just used that instead of relying on a name

  2. When the SelectionUnit is FullRow, as my grid was before I changed it to CellOrRowHeader you
    apparently can't call SelectedCells.Clear()


Code below:



private void OnDataGridKeyboardGotFocus(object sender, KeyboardFocusChangedEventArgs e)
{
var dg = sender as DataGrid;
if (_hasHadInitialFocus) return;

var rowIndex = dg.Items.Count - 2;
if (rowIndex >= 0 && dg.Columns.Count - 1 >= 0)
{
var column = dg.Columns[dg.Columns.Count - 1];
var item = dg.Items[rowIndex];
var dataGridCellInfo = new DataGridCellInfo(item, column);

if (dg.SelectionUnit != DataGridSelectionUnit.FullRow) {
dg.SelectedCells.Clear();
dg.SelectedCells.Add(dataGridCellInfo);
}
else {
var row = dg.GetRow(rowIndex);
row.IsSelected = true;
}

dg.CurrentCell = dataGridCellInfo;
dg.BeginEdit();
}

_hasHadInitialFocus = true;
}


New Question



I want to repeat the selection when the focus goes to another control in the window and then back to the grid.
I thought I could turn that _hasHadInitialFocus latch to false in a LostFocus event, but the code below is firing on cell changes.
Do you know how I should be trapping the lost focus event better, and do you agree that is the place to turn the latch off?



    private void DataGridLostFocus(object sender, RoutedEventArgs e) {
_hasHadInitialFocus = false;
}






wpf datagrid focus






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 7:50









Cœur

17.8k9107146




17.8k9107146










asked Mar 3 '12 at 3:25









BerrylBerryl

6,7701779162




6,7701779162













  • You could perhaps look at e.NewFocus, e.OldFocus, e.OriginalSource in DataGridLostFocus and do something clever with those. I'm afraid my source of info on the DataGrid is probably the same as yours. MSDN, StackOverflow and plenty of experimentation.

    – Phil
    Mar 4 '12 at 9:35



















  • You could perhaps look at e.NewFocus, e.OldFocus, e.OriginalSource in DataGridLostFocus and do something clever with those. I'm afraid my source of info on the DataGrid is probably the same as yours. MSDN, StackOverflow and plenty of experimentation.

    – Phil
    Mar 4 '12 at 9:35

















You could perhaps look at e.NewFocus, e.OldFocus, e.OriginalSource in DataGridLostFocus and do something clever with those. I'm afraid my source of info on the DataGrid is probably the same as yours. MSDN, StackOverflow and plenty of experimentation.

– Phil
Mar 4 '12 at 9:35





You could perhaps look at e.NewFocus, e.OldFocus, e.OriginalSource in DataGridLostFocus and do something clever with those. I'm afraid my source of info on the DataGrid is probably the same as yours. MSDN, StackOverflow and plenty of experimentation.

– Phil
Mar 4 '12 at 9:35












1 Answer
1






active

oldest

votes


















1














You may have to fiddle with the offsets depending on whether there's an new item row visible or not, but this works for me.



    private bool _hasHadInitialFocus;

private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (!_hasHadInitialFocus)
{
if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
{
var dataGridCellInfo = new DataGridCellInfo(
dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);

dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;
dataGrid.BeginEdit();
}

_hasHadInitialFocus = true;
}
}


I noticed that clicking into the grid leaves one cell selected and the target cell in edit mode. A solution to this if required is:



    private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
EditCell();
}

private void PreviewMouseLBDown(object sender, MouseButtonEventArgs e)
{
if (!_hasHadInitialFocus)
{
e.Handled = true;
EditCell();
}
}

private void EditCell()
{
if (!_hasHadInitialFocus)
{
if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
{
var dataGridCellInfo = new DataGridCellInfo(
dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);

dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;
dataGrid.BeginEdit();
}

_hasHadInitialFocus = true;
}
}





share|improve this answer


























  • Great code - please see my edited post for some comments and a follow up question or three :--). Cheers

    – Berryl
    Mar 4 '12 at 0:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9543145%2fdatagrid-select-last-cell-programmatically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You may have to fiddle with the offsets depending on whether there's an new item row visible or not, but this works for me.



    private bool _hasHadInitialFocus;

private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (!_hasHadInitialFocus)
{
if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
{
var dataGridCellInfo = new DataGridCellInfo(
dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);

dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;
dataGrid.BeginEdit();
}

_hasHadInitialFocus = true;
}
}


I noticed that clicking into the grid leaves one cell selected and the target cell in edit mode. A solution to this if required is:



    private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
EditCell();
}

private void PreviewMouseLBDown(object sender, MouseButtonEventArgs e)
{
if (!_hasHadInitialFocus)
{
e.Handled = true;
EditCell();
}
}

private void EditCell()
{
if (!_hasHadInitialFocus)
{
if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
{
var dataGridCellInfo = new DataGridCellInfo(
dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);

dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;
dataGrid.BeginEdit();
}

_hasHadInitialFocus = true;
}
}





share|improve this answer


























  • Great code - please see my edited post for some comments and a follow up question or three :--). Cheers

    – Berryl
    Mar 4 '12 at 0:27
















1














You may have to fiddle with the offsets depending on whether there's an new item row visible or not, but this works for me.



    private bool _hasHadInitialFocus;

private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (!_hasHadInitialFocus)
{
if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
{
var dataGridCellInfo = new DataGridCellInfo(
dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);

dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;
dataGrid.BeginEdit();
}

_hasHadInitialFocus = true;
}
}


I noticed that clicking into the grid leaves one cell selected and the target cell in edit mode. A solution to this if required is:



    private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
EditCell();
}

private void PreviewMouseLBDown(object sender, MouseButtonEventArgs e)
{
if (!_hasHadInitialFocus)
{
e.Handled = true;
EditCell();
}
}

private void EditCell()
{
if (!_hasHadInitialFocus)
{
if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
{
var dataGridCellInfo = new DataGridCellInfo(
dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);

dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;
dataGrid.BeginEdit();
}

_hasHadInitialFocus = true;
}
}





share|improve this answer


























  • Great code - please see my edited post for some comments and a follow up question or three :--). Cheers

    – Berryl
    Mar 4 '12 at 0:27














1












1








1







You may have to fiddle with the offsets depending on whether there's an new item row visible or not, but this works for me.



    private bool _hasHadInitialFocus;

private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (!_hasHadInitialFocus)
{
if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
{
var dataGridCellInfo = new DataGridCellInfo(
dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);

dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;
dataGrid.BeginEdit();
}

_hasHadInitialFocus = true;
}
}


I noticed that clicking into the grid leaves one cell selected and the target cell in edit mode. A solution to this if required is:



    private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
EditCell();
}

private void PreviewMouseLBDown(object sender, MouseButtonEventArgs e)
{
if (!_hasHadInitialFocus)
{
e.Handled = true;
EditCell();
}
}

private void EditCell()
{
if (!_hasHadInitialFocus)
{
if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
{
var dataGridCellInfo = new DataGridCellInfo(
dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);

dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;
dataGrid.BeginEdit();
}

_hasHadInitialFocus = true;
}
}





share|improve this answer















You may have to fiddle with the offsets depending on whether there's an new item row visible or not, but this works for me.



    private bool _hasHadInitialFocus;

private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (!_hasHadInitialFocus)
{
if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
{
var dataGridCellInfo = new DataGridCellInfo(
dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);

dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;
dataGrid.BeginEdit();
}

_hasHadInitialFocus = true;
}
}


I noticed that clicking into the grid leaves one cell selected and the target cell in edit mode. A solution to this if required is:



    private void DataGridGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
EditCell();
}

private void PreviewMouseLBDown(object sender, MouseButtonEventArgs e)
{
if (!_hasHadInitialFocus)
{
e.Handled = true;
EditCell();
}
}

private void EditCell()
{
if (!_hasHadInitialFocus)
{
if (dataGrid.Items.Count - 2 >= 0 && dataGrid.Columns.Count - 1 >= 0)
{
var dataGridCellInfo = new DataGridCellInfo(
dataGrid.Items[dataGrid.Items.Count - 2], dataGrid.Columns[dataGrid.Columns.Count - 1]);

dataGrid.SelectedCells.Clear();
dataGrid.SelectedCells.Add(dataGridCellInfo);
dataGrid.CurrentCell = dataGridCellInfo;
dataGrid.BeginEdit();
}

_hasHadInitialFocus = true;
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 3 '12 at 16:05

























answered Mar 3 '12 at 9:35









PhilPhil

34.6k57791




34.6k57791













  • Great code - please see my edited post for some comments and a follow up question or three :--). Cheers

    – Berryl
    Mar 4 '12 at 0:27



















  • Great code - please see my edited post for some comments and a follow up question or three :--). Cheers

    – Berryl
    Mar 4 '12 at 0:27

















Great code - please see my edited post for some comments and a follow up question or three :--). Cheers

– Berryl
Mar 4 '12 at 0:27





Great code - please see my edited post for some comments and a follow up question or three :--). Cheers

– Berryl
Mar 4 '12 at 0:27


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9543145%2fdatagrid-select-last-cell-programmatically%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith