下拉列表过滤数据表
本文关键字:数据表 过滤 下拉列表 | 更新日期: 2023-09-27 18:13:32
我有一个包含4列的表。我希望能够做的是根据多个DropDownList的选择来过滤这个数据表。
如何删除不包含选择的行?我已经使用SQL填充数据表,但我不想调用SQL来过滤数据表?
下面是我如何添加数据表和填充下拉列表的代码private DataTable LoadDataTable()
{
//conn.Open();
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM DataTest", conn);
adapter.Fill(dt);
conn.Close();
return dt;
}
public void PopulateDDL()
{
SqlCommand cmd = new SqlCommand("SELECT DISTINCT Column1 FROM DataTest", conn);
cmd.Connection.Open();
SqlDataReader ddlValues;
ddlValues = cmd.ExecuteReader();
DropDownList1.DataSource = ddlValues;
DropDownList1.DataValueField = "Column1";
DropDownList1.DataTextField = "Column1";
DropDownList1.DataBind();
cmd.Connection.Close();
}
不管怎样,我可以去做吗?
你可以试试
// Presuming the DataTable has a column named List.
string expression;
expression = "List != TV";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);
看看这个数据表信息网站,更具体地说,这个数据表的jquery扩展"Yet Another datatables Column Filter"
链接包含一个实时示例
这是生意!
如果您希望根据Multidropdown中的多个值选择来过滤数据表,您可以使用以下代码来实现这一点。假设多重下拉列表包含id为chkMultiSelection
asp:CheckboxList
假设你有System DataTable dt
上的所有数据,需要根据MultiDropDownlist
var dataSet = new DataSet();
var filterDataTable = new DataTable();
//get all the selection of MultiDropDown and seperated them by comas..
string selectedValues = string.Join(", ", chkMultiSelection.Items.Cast<ListItem>().Where(x => x.Selected).Select(x => x.Text));
//convert your multiselection to array
string[] multiValues = selectedValues.Split(',');
//iterate through them and filter the data based on selection. That will filter the rows which don't contain the selection
foreach (string s in multiValues)
{
IEnumerable<DataRow> datarow = default(IEnumerable<DataRow>);
datarow = dt.AsEnumerable().Where(x => x.Field<string>("ColumnName") == s.Trim());
if (datarow.Count() > 0)
{
filterDataTable = datarow.CopyToDataTable();
//use dataset and store each filter data
dataSet.Tables.Add(filterDataTable);
}
}