简体   繁体   中英

Select DataGridView row when searching

I am searching through a DataGridView. The TextChanged event can find the data, but the cursor does not jump to found row/column.

This is the powershell code I am using:

$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.

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