2011-10-20 11 views
11

Ho una vista Datagrid e Data Source è dtCustomer Voglio solo filtrare il contenuto della vista griglia in base a un testo di ricerca. Itried il seguente codiceDatatable Select() Method

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

Ma questo non sta funzionando. Se qualche organismo conosce la soluzione, si prega di condividere.

risposta

11

Prova questa:

dtSearch.DefaultView.RowFilter = "cust_Name like '" + txtSearch.Text + "%'"; 

e verificare quello che c'è spazio per essere rimosso dal triming il testo.

3

Si potrebbe provare a utilizzare un DataView (codice non testato) -

DataView dv = new DataView(dtSearch); 
dv.RowFilter = "cust_Name like '" + txtSearch.Text + "%'"; 
grvCustomer.DataSource = dv; 
8

Il valore restituito per DataTable.Select è un [] array DataRow. Restituisce un elenco di DataRows corrispondenti. Il tuo codice non fa nulla con quelle file al momento.

È possibile impostare una DataView con un filtro e impostare DataSource della griglia al DataView:

DataView dv = new DataView(dtSearch); 
dv.RowFilter = "..."; 
grvCustomer.DataSource = dv; 
1

DataTable.Select ritorna gamma di fila, ma si sono vincolanti intera tabella dati righe non filtrati. utilizzare in questo modo o DataView

DataTable dtSearch = dtCustomer; 
var filter = dtSearch.Select("cust_Name like '" + txtSearch.Text + "%'"); 
grvCustomer.DataSource = filter.ToList(); 
2

Oppure Provare questo;

dataGridView.Datasource = datatable.Select("....").CopyToDataTable() 
0

Penso che questo sia quello che stai cercando?

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


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

E quando si vuole tornare ai dati originali

grvCustomer.DataSource = dtCustomer; 
-1
dtCustomer.Rows.Cast<DataRow>().Select(dr => (string)dr["cust_Name"].Startswith("zzz")).ToList() 
1

Si può fare qualcosa di simile.

DataView dv1 = dtDefault.DefaultView; 
dv1.RowFilter = "CusGroupId=1 and CustomerCode LIKE '"+txtCustomer.Text +"%'"; 
DataTable dt=dv1.ToTable();