10

我想过滤 listview 上 textchange 事件的数据,所以我使用 dataview 过滤数据。下面代码中的问题是,我在每个内部都使用 dataview,以便它只检查一个条件,即只有它需要的最后一个值,我想用 dataview 检查s1中的值,剩余的值应该与 listview 绑定。

例如:如果我在文本框中键入一个,它应该列出所有以 anandha kumar,anna 等值开头的项目值。假设我将值 anandha kumar 和 anna 保留在数组 s1 中。我应该在列表视图中列出所有其他值,除了数组值,如安东尼等。

protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            dvProducts = (DataView)Session["ListViewItems"];

            string serachText = EscapeLikeValue(TextBox1.Text);

            string lvValues = hdRetailCustomerGroup.Value;

            string trim = lvValues.Replace(" ", "");

            trim = trim.Replace("\r", "");

            trim = trim.Replace("\n", "");

            trim = trim.Replace("\t", "");
             string str = trim;

            string[] list = str.Split('|');


            foreach (string s1 in list)
            {
                if (s1 != string.Empty)
                {
                    dvProducts.RowFilter = "(CODE like '" + serachText + "*') AND (CODE <> '" + s1 + "')";
                    Session["ListViewItems"] = dvProducts;
                }
            }

                       ListView1.DataSource = dvProducts;
                     ListView1.DataBind();

        }
4

2 回答 2

18
DataView view = new DataView();
view.Table = DataSet1.Tables["Suppliers"];
view.RowFilter = "City = 'Berlin'";
view.RowStateFilter = DataViewRowState.ModifiedCurrent;
view.Sort = "CompanyName DESC";

// Simple-bind to a TextBox control
Text1.DataBindings.Add("Text", view, "CompanyName");

参考: http ://www.csharp-examples.net/dataview-rowfilter/

http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx

于 2012-04-04T11:02:34.903 回答
7

例如:

Datatable newTable =  new DataTable();

            foreach(string s1 in list)
            {
                if (s1 != string.Empty) {
                    dvProducts.RowFilter = "(CODE like '" + serachText + "*') AND (CODE <> '" + s1 + "')";
                    foreach(DataRow dr in dvProducts.ToTable().Rows)
                    {
                       newTable.ImportRow(dr);
                    }
                }
            }
ListView1.DataSource = newTable;
ListView1.DataBind();
于 2012-04-04T11:00:39.850 回答