「ナビゲート」ソリューション
private void textBox1_TextChanged(object sender, EventArgs e)
{
for (int i=0;i<theDataGridView.RowCount;i++)
{
if (theDataGridView.Rows[i].Cells["surname"].Value.ToString().StartsWith(textBox1.Text))
{
theDataGridView.CurrentCell = theDataGridView.Rows[i].Cells["surname"];
// optionally
theDataGridView.FirstDisplayedScrollingRowIndex = theDataGridView.CurrentCell.RowIndex;
break ;
}
}
}
「フィルター」ソリューション
まず、BindingSource を介して DataTable を DataGridView にバインドします。
BindingSource theBindingSource = new BindingSource() ;
theBindingSource.DataSource = theDataTable ;
theDataGridView.DataSource = theBindingSource ;
次に、TextChanged イベントで BindingSource の Filter プロパティを設定します。
private void textBox1_TextChanged(object sender, EventArgs e)
{
theBindingSource.Filter = "surname LIKE '"+textBox1.Text+"%'";
// if no match : Cancel filter
if (theDataGridView.RowCount==0) theBindingSource.Filter = "" ;
}