0

データ アダプター ( ) を使用してフォームにデータDataGridViewが入力されるフォームがあります。これは、人のと(データベース内の基になるテーブルの主キー) を示しています。ただし、行はフィールド別に並べ替えられます (データ アダプターに渡されるステートメントの句を使用することにより)。ConstructorDataSourceAdsDataAdapterfirst namesurnamereference numberDataGridViewsurnameORDER BYSELECT

が入力された後、 aに aを入力して、ユーザーが入力したものと一致する の最初の文字が最初の行に移動DataGridViewできるようにしたいと考えています。surnameTextBoxDataGridViewsurname

このナビゲーションを行うにはどうすればよいですか?

4

1 に答える 1

1

「ナビゲート」ソリューション

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 = "" ; 
}
于 2015-07-21T22:32:42.643 に答える