0

我有一个包含 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();            
}

反正我可以去吗?

4

3 回答 3

0

好的你可以试试

// 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);
于 2013-05-15T08:40:18.853 回答
0

看看这个数据信息网站,更具体地说,这个数据表的 jquery 扩展“Yet Another DataTables Column Filter”

该链接包含一个实时示例

是生意!

于 2013-05-15T08:41:04.207 回答
0

如果要根据 Multidropdown 中的多个值选择来过滤数据表,可以使用以下代码来实现。假设您的 multidropdown 包含asp:CheckboxListidchkMultiSelection

假设您拥有所有数据,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);

    }
}
于 2017-02-10T06:14:36.467 回答