How do I use the Datagrid.SelectedItem
to select a row programmatically?
Do I first have to create a IEnumerable
of DataGridRow
objects and pass the matching row to this SelectedItem
property or how do I do it?
EDIT:
I need to match the cell content of the first columns cell with a TextBox.Text
first, before selecting the row.
My code iterates through cells of the datagrid
's first column and checks if cell content equals to the textbox.text
value and selects the row.
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;
dataGrid.ScrollIntoView(item);
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
break;
}
}