mardi, décembre 20, 2005

DataView & DataTable, implemente search critaria

Imagine that we have a DataTable filled with all informations needed, and we want to filter the output at the run time. For example we have a Client table where there is ClientID, ClientName, Adress, Country, Tel, Fax...; at the design time I had put on the form a Dataset (MyDataset), combobox (comboboxClient) and a listView(ClientList), I want to select Clients where the coutry is selected by the combobox, that is an easy task : at the Click event of the combobox, i will the code bellow :

DataView view = new DataView();
view.Table = this.MyDataset.Tables["Client"];
view.RowFilter = "Country = '" + this.ComboBoxClient.SelectedItem.ToString().Trim() + "'";


foreach (DataRowView rowView in view)

ListViewItem listItem = new ListViewItem(rowView["ClientName"].ToString().Trim());


this.ClientList.Columns.Add("Client name", 270, HorizontalAlignment.Left);
this.ClientList.Columns.Add("Adress", 200, HorizontalAlignment.Left);
this.ClientList.Columns.Add("Tel", 200, HorizontalAlignment.Left);

Aucun commentaire: