【发布时间】:2016-04-11 21:18:03
【问题描述】:
当用户单击我的 datagridview 的一个列并选择过滤时,会弹出一个窗口,其中包含一个列表框,其中填充了该列的值(不重复——如果有 5 个 0,则它只显示一次)。
这是弹出窗口的初始化。
public partial class ComboBoxFilter : Form
{
DataGridView Dgv;
DataGridViewColumn Col;
DataView View;
string CName;
public ComboBoxFilter(DataGridView dgv, DataGridViewColumn col, DataView view, string colName)
{
InitializeComponent();
Dgv = dgv;
Col = col;
View = view;
CName = colName;
listBox1.ValueMember = col.DataPropertyName;
listBox1.DisplayMember = col.DataPropertyName;
DataTable dt = view.ToTable(true, new string[] { col.DataPropertyName });
dt.DefaultView.Sort = col.DataPropertyName;
listBox1.ClearSelected();
listBox1.DataSource = dt;
}
当用户从列表框中选择一个值并按下确定按钮时:
private void buttonOK_Click(object sender, EventArgs e)
{
BindingSource bs = (BindingSource)Dgv.DataSource;
bs.Filter = string.Format("{0} = '{1}'", CName, listBox1.SelectedValue.ToString());
Dgv.DataSource = bs;
this.Close();
}
其中 CName 是要过滤的列名称。
这很好用。
但是现在我想在我的列表框上允许多选属性,这样如果用户选择了多个值,我可以对其进行过滤。我怎样才能做到这一点?有必要像我在一些例子中看到的那样使用“OR”吗?
【问题讨论】:
-
将控件移动到表单构造函数中让我毛骨悚然。
标签: c# .net datagridview listbox filtering