What is the most efficient way to filter lists based on other selection in C #?

I have multiple lists that get each of their data from a separate stored procedure.

If a user selects an option in 1 list, he must filter other lists.

I've done this before adding logic to the stored procedure, but sometimes it seems very long.

Does anyone know how best to approach this?

Now I have a setup for each ListBox, I have an ObjectDataSource that calls a method that calls a stored proc in the database to populate the list.

0


source to share


2 answers


You can try changing the code so that instead of binding the Listbox directly to the ADO.Net datatable, it binds to the DataView. DataViews can be sorted and filtered independently of the underlying DataTable they are based on ...

Suppose LBStates is a ListBox state and lbCities is a City ListBox and dtCities is a DataTable of the form with all the cities in it and it has a State Column ...

     DataView dvCities = dtCities.DefaultView; 
     dvCities.RowFilter = "State=" + lbStates.SelectedItem;
     lbCities.DataSource = dvCities;

      

Hook up the selectedIndexChanged event to the ListBox (in initialization code)



 lbStates.SelectedIndexChanged += lbStates_SelectedIndexChanged;

      

and in the ListBox state SelectedIndexChanged add the same code ...

  private void lbStates_SelectedIndexChanged(object sender, event e)
  {
     DataView dvCities = dtCities.DefaultView; 
     dvCities.RowFilter = "State=" + lbStates.SelectedItem;
     lbCities.DataSource = dvCities;
  }

      

+2


source


Lists are often used to display "search" data that does not change frequently. As a list of states or types of objects. So, one thing to learn when trying to improve performance is caching. There is no reason for a two-way trip to the database every time you want to get a list of states.



Also if you want to return all the list data in one database call and store in a strongly typed dataset. Then you can filter the content of the dataset based on the list selection and simply reinstall the content of the dataset to other lists.

+1


source







All Articles