Search for value in DataGridView in a column

前端 未结 6 1435
名媛妹妹
名媛妹妹 2020-12-01 17:12

I want the user to be able to search for a number in a column in the DataGridView (dgv). The dgv can hold many records. Each record has a Project Number. So I want the user

6条回答
  •  悲哀的现实
    2020-12-01 18:12

    Why don't you build a DataTable first then assign it to the DataGridView as DataSource:

    DataTable table4DataSource=new DataTable();
    
    table4DataSource.Columns.Add("col00");
    table4DataSource.Columns.Add("col01");
    table4DataSource.Columns.Add("col02");
    
    ...
    

    (add your rows, manually, in a circle or via a DataReader from a database table) (assign the datasource)

    dtGrdViewGrid.DataSource = table4DataSource;
    

    and then use:

    (dtGrdViewGrid.DataSource as DataTable).DefaultView.RowFilter = "col00 = '" + textBoxSearch.Text+ "'";
    dtGrdViewGrid.Refresh();
    

    You can even put this piece of code within your textbox_textchange event and your filtered values will be showing as you write.

提交回复
热议问题