简体   繁体   English

如何以编程方式在数据网格中选择行?

[英]How can select row in datagrid programmatically?

I want select row in wpf datagrid automatically when textbox lost focus.我想在文本框失去焦点时自动选择 wpf 数据网格中的行。 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当我运行此代码时,所选索引始终 = -1

This is select the data grid row number 2. as index begin with 0这是选择数据网格行号 2。因为索引从 0 开始

 int rowNumber =1;
 dataGrid.SelectedIndex = rowNumber;

value of the rowNumber should always be less than or equal to number of rows in datagridview rowNumber 的值应始终小于或等于 datagridview 中的行数

You need to select a DataGridViewCell: http://social.msdn.microsoft.com/Forums/windows/en-US/1f5d313a-203d-4e7a-ac46-eee65347a9f4/datagridview-select-cell您需要选择一个 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.看起来您想要过滤列表,所以我建议您改用 CollectionViewSource(或类似的)和数据绑定。

Search for DataBinding and DataGrid搜索 DataBinding 和 DataGrid

Try this:尝试这个:

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

If you know the index, it should work.如果您知道索引,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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