简体   繁体   中英

How can select row in datagrid programmatically?

I want select row in wpf datagrid automatically when textbox lost focus. How can I do that? I use this code but this code is not running?

private void txtBox_LostFocus(object sender, RoutedEventArgs e)
    {
        dataGrid.SelectedIndex = 'I found index from table'
    }

When I run this code selected index is always = -1

This is select the data grid row number 2. as index begin with 0

 int rowNumber =1;
 dataGrid.SelectedIndex = rowNumber;

value of the rowNumber should always be less than or equal to number of rows in datagridview

You need to select a DataGridViewCell: http://social.msdn.microsoft.com/Forums/windows/en-US/1f5d313a-203d-4e7a-ac46-eee65347a9f4/datagridview-select-cell

It looks like you want Filter a list, so I would recommend you to use a CollectionViewSource (or similar) and Databinding instead.

Search for DataBinding and DataGrid

Try this:

private void txtBox_LostFocus(object sender, RoutedEventArgs e)
    {
        dataGrid.Rows(index).Selected = True
    }

If you know the index, it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM