Datatable Select() Method

前端 未结 8 1637
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-11 16:15

I have a Datagridview and the Data Source is dtCustomer I just want to filter the content of grid view based on a search text. Itried the follow

相关标签:
8条回答
  • 2020-12-11 16:29

    You could try using a DataView (code not tested) -

    DataView dv = new DataView(dtSearch);
    dv.RowFilter = "cust_Name like '" + txtSearch.Text + "%'";
    grvCustomer.DataSource = dv;
    
    0 讨论(0)
  • 2020-12-11 16:29

    Or Try this;

    dataGridView.Datasource = datatable.Select("....").CopyToDataTable()
    
    0 讨论(0)
  • 2020-12-11 16:30

    I think this is what you're looking for?

    //DataTable dtSearch =  dtCustomer;
    //dtSearch.Select("cust_Name like '" + txtSearch.Text + "%'");
    
    
    grvCustomer.DataSource = dtCustomer.Select("cust_Name like '" + txtSearch.Text + "%'");
    

    And when you want to go back to the original data

    grvCustomer.DataSource = dtCustomer;
    
    0 讨论(0)
  • 2020-12-11 16:36
    dtCustomer.Rows.Cast<DataRow>().Select(dr => (string)dr["cust_Name"].Startswith("zzz")).ToList()
    
    0 讨论(0)
  • 2020-12-11 16:37

    The return value for DataTable.Select is a DataRow[] array. It returns a list of matching DataRows. Your code does nothing with those rows at the moment.

    You can setup a DataView with a filter and set the grid's DataSource to the DataView:

    DataView dv = new DataView(dtSearch);
    dv.RowFilter = "...";
    grvCustomer.DataSource = dv;
    
    0 讨论(0)
  • 2020-12-11 16:41

    DataTable.Select returns array of row, but you are binding entire data table not filtered rows. use this way or DataView

    DataTable dtSearch =  dtCustomer;
    var filter = dtSearch.Select("cust_Name like '" + txtSearch.Text + "%'");
    grvCustomer.DataSource = filter.ToList();
    
    0 讨论(0)
提交回复
热议问题