简体   繁体   English

搜索时选择DataGridView行

[英]Select DataGridView row when searching

I am searching through a DataGridView. 我正在搜索DataGridView。 The TextChanged event can find the data, but the cursor does not jump to found row/column. TextChanged事件可以找到数据,但是光标不会跳到找到的行/列。

This is the powershell code I am using: 这是我正在使用的powershell代码:

$ColNameNow = "Name"
$SrcValue = $TextBox1.text

for ($i = 0; $i -lt $datagridview1.RowCount; $i++)
{
    if ($datagridview1.Rows[$i].Cells['Name'].Value -eq "$SrcValue")
    {
        [System.Windows.Forms.MessageBox]::Show("Found", "Update", "Ok", [System.Windows.Forms.MessageBoxIcon]::Warning)

        $datagridview1.CurrentRow.Index = $i            
        $datagridview1.CurrentRow = $datagridview1.Rows[$i].Cells[0]            
        $datagridview1.SelectedRows[$i]         
    }
}

Replace the following lines: 替换以下行:

$datagridview1.CurrentRow.Index = $i            
$datagridview1.CurrentRow = $datagridview1.Rows[$i].Cells[0]            
$datagridview1.SelectedRows[$i]

With the following: 具有以下内容:

$datagridview1.CurrentCell = $datagridview1.Rows[$i].Cells['Name']            
$datagridview1.FirstDisplayedScrollingRowIndex = $i

Setting the CurrentCell highlights the correct cell and setting the FirstDisplayedScrollingRowIndex sets the selected row to be the top displayed row if possible. 设置CurrentCell突出显示正确的单元格,而设置FirstDisplayedScrollingRowIndex则将所选行设置为显示的顶部行(如果可能)。

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

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