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() + "'";

this.ClientList.BeginUpdate();
this.ClientList.Clear();

foreach (DataRowView rowView in view)
{

ListViewItem listItem = new ListViewItem(rowView["ClientName"].ToString().Trim());
listItem.SubItems.Add(rowView["Adress"].ToString().Trim());
listItem.SubItems.Add(rowView["Tel"].ToString().Trim());
this.ClientList.Items.Add(listItem);

}

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);
this.ClientList.EndUpdate();

Aucun commentaire: