Click here to Skip to main content
15,798,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm Using WPF C# in Visual Studio and using Entity Framework

My DataGrid Binding a Table in Database of SQL Server with Data

when user click on a row in DataGrid that row will show in the textbox and Combobox

the ID of row clicked is in ID_TextBox

how can i select and focus on a row by id in TextBox

What I have tried:

for (int i = 0; i < dataGrid.Items.Count; i++)
    DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
    TextBlock cellContent = dataGrid.Columns[0].GetCellContent(row) as TextBlock;
    if (cellContent != null && cellContent.Text.Equals(textBox1.Text))
        object item = dataGrid.Items[i];
        dataGrid.SelectedItem = item;
        row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
Updated 11-Dec-20 5:20am
Gerry Schmitz 4-Dec-20 9:03am    
Well? You looked for an item, apparently find it, set SelectedItem ... and what? What did or do not happen?

1 solution

You could set SelectedIndex to the index of the found item.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900