根据c#中的另一个选择,最有效的方法来过滤列表框? [英] What is the most efficient way to filter listboxes based on selections of another in c#?

查看:169
本文介绍了根据c#中的另一个选择,最有效的方法来过滤列表框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个列表框,从单独的存储过程中获取每个数据。



如果用户在1列表框中选择一个选项,它应该过滤其他列表框。



之前我已经通过向存储过程添加逻辑,但有时似乎很长时间。有没有人知道一个更好的方法来处理这个问题?





我现在设置的方式是,对于每个ListBox,我有一个ObjectDataSource,它调用一个方法来调用数据库中的存储过程来填充列表框。 / p>

解决方案

您可以尝试更改代码,而不是让列表框直接绑定到ADO.Net datatable,它绑定到DataView。 DataView可以独立于基于它们的基础DataTable进行排序和过滤...



假设LBStates是状态ListBox,lbCities是City ListBox,dtCities是形式级别DataTable变量与它所有的城市,它有一个State列...

  DataView dvCities = dtCities.DefaultView; 
dvCities.RowFilter =State =+ lbStates.SelectedItem;
lbCities.DataSource = dvCities;把一个selectedIndexChanged事件连接到States ListBox(在初始化代码中)



$ p
$ b

  lbStates.SelectedIndexChanged + = lbStates_SelectedIndexChanged; 

并在States ListBox SelectedIndexChanged事件中添加相同的代码...

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


I have several listboxes that get each of their data from a separate stored procedure.

If the user selects an option in 1 listbox, it should filter the other listboxes.

I have done this before by adding logic to the stored procedure, but sometimes it seems to get very long.

Does anyone know of a better way to approach this?

The way I have it setup now is that for each ListBox, I have an ObjectDataSource which calls a method that calls a stored proc in the database to populate the listbox.

解决方案

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

Assume LBStates is state ListBox, and lbCities is City ListBox, and dtCities is form level DataTable variable with all cities in it and it has a State Column...

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

Wire up a selectedIndexChanged event to the States ListBox (in initializtion code)

 lbStates.SelectedIndexChanged += lbStates_SelectedIndexChanged;

and In the States ListBox SelectedIndexChanged event, add the same code...

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

这篇关于根据c#中的另一个选择,最有效的方法来过滤列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆